0

以下 for 循环转储整个文件 (.txt) 而不是第一行是什么?

对于循环:

counter = 0

for line in file:
    while counter < 1:
        counter = counter + 1
        print line.split()

文件格式:

name    city    state   phone   certifications  areas_of_practice   languages   lat lng
Bxxxx, Mxxxx F  Birmingham   AL (123) 555-2281  NCC Clinical Mental Health, Counselor Education, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling English 99.52029    -99.8115
Axxxx, Axxxx Brown  Birmingham   AL (123) 555-2281  NCC Clinical Mental Health, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling English 99.52029    -99.8115
Axxxx, Bxxxx    Mobile   AL (123) 555-8011  NCC Childhood & Adolescence, Clinical Mental Health, Sexual Abuse Recovery, Disaster Counseling English 99.68639    -99.053238
Axxxx, Rxxxx Lunsford   Athens   AL (123) 555-8119  NCC, NCCC, NCSC Career Development, Childhood & Adolescence, School, Disaster Counseling, Supervision   English 99.804501   -99.971283
Axxxx, Mxxxx    Mobile   AL (123) 555-5963  NCC Clinical Mental Health, Counselor Education, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling, Supervision   English 99.68639    -99.053238
Axxxx, Txxxx    Mountain Brook   AL (123) 555-3099  NCC Addictions and Dependency, Career Development, Childhood & Adolescence, Corrections/Offenders, Sexual Abuse Recovery    English 99.50214    -99.75557
Axxxx, Lxxxx    Birmingham   AL (123) 555-4550  NCC Addictions and Dependency, Eating Disorders English 99.52029    -99.8115
Axxxx, Wxxxx    Birmingham   AL (123) 555-2328  NCC     English 99.52029    -99.8115
Axxxx, Rxxxx    Mobile   AL (123) 555-9411  NCC Addictions and Dependency, Childhood & Adolescence, Couples & Family, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill English 99.68639    -99.053238
Axxxx, Jxxxx    Alabaster    AL (123) 555-1600  NCC Addictions and Dependency, Childhood & Adolescence, Couples & Family, Mental Health/Agency Counseling, Disaster Counseling  English 99.245079   -99.816223
Bxxxx, Jxxxx Pxxx   Florence     AL (123) 555-4667  NCC Career Development, Counselor Education English 99.800449   -99.675262
Bxxxx, Sxxxx    Huntsville   AL (123) 555-9000  NCC Childhood & Adolescence, Couples & Family, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill    English 99.72913    -99.584976
Bxxxx, Kxxxx    Dothan   AL (123) 555-2865  NCC Addictions and Dependency, Clinical Mental Health, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill, Disaster Counseling   English 99.223221   -99.393372
Bxxxx, Mxxxx    Guntersville     AL (123) 555-6225  NCC Childhood & Adolescence, Clinical Mental Health, Aging/Gerontological, Mental Health/Agency Counseling, Disaster Counseling English 99.351131   -99.299156
Bxxxx, Sxxxx    Montgomery   AL (123) 555-4851  NCC Career Development, Counselor Education, Supervision    English 99.380119   -99.300629
Bxxxx, Dxxxx    Huntsville   AL (123) 555-3231  NCC, CCMHC  Childhood & Adolescence, Clinical Mental Health, Couples & Family, Depression/Grief/Chronically or Terminally Ill, Supervision  English 99.72913    -99.584976
Bxxxx, Rxxxx    Mobile   AL (123) 555-7344  NCC, MAC    Addictions and Dependency, Childhood & Adolescence, Clinical Mental Health, Couples & Family, Depression/Grief/Chronically or Terminally Ill    English 99.68639    -99.053238
Bxxxx, Txxxx    Auburn   AL (123) 555-1629  NCC Childhood & Adolescence, Clinical Mental Health, Couples & Family, Sexual Abuse Recovery, Mental Health/Agency Counseling   English 99.60746    -99.481728
Bxxxx, Kxxxx Gxxx   Homewood     AL (123) 555-9663  NCC, NCSC   Counselor Education, School English 99.478771   -99.789673
Bxxxx, Jxxxx, Sxxx  Mobile   AL (123) 555-7622  NCC Childhood & Adolescence, Clinical Mental Health, Couples & Family, Mental Health/Agency Counseling  English 99.68639    -99.053238
Bxxxx, Txxxx    Birmingham   AL (123) 555-1483  NCC Childhood & Adolescence, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling    English 99.52029    -99.8115
Bxxxx, Axxxx    Birmingham   AL (123) 555-3733  NCC Addictions and Dependency, Career Development, Corrections/Offenders, Sexual Abuse Recovery, Mental Health/Agency Counseling    English 99.52029    -99.8115
Bxxxx, Axxxx    Florence     AL (123) 555-1493  NCC Addictions and Dependency, Childhood & Adolescence, Clinical Mental Health, Corrections/Offenders, Supervision  English 99.800449   -99.675262
Bxxxx, Axxxx    Tuscaloosa   AL (123) 555-5848  NCC Career Development, Rehabilitation  English 99.210491   -99.565933
Bxxxx, Jxxxx A  Tuscaloosa   AL     NCC Childhood & Adolescence, Couples & Family, Mental Health/Agency Counseling  English 99.210491   -99.565933
Bxxxx, Cxxxx    Daphne   AL (123) 555-0118  NCC Addictions and Dependency, Clinical Mental Health, Couples & Family, Depression/Grief/Chronically or Terminally Ill, Supervision    English 99.603901   -99.901512
Bxxxx, Txxxx    Florence     AL (123) 555-2240  NCC     English 99.800449   -99.675262
4

2 回答 2

2

假设它f被实例化为open("file.txt", "r"),那么你只是简单地对文件进行迭代。

如果您只想要第一行之后的元素,请尝试以下构造:

with(open("file.txt", "r")) as f:
    li = f.readlines()
    for line in li[1:]:
        print line.split()
于 2013-06-28T13:44:44.087 回答
1

如果您想要的只是第一行(例如获取标题),那么这应该可以解决问题:

with open('file.txt', 'rb') as f:
    print f.readline().split()
于 2013-06-28T13:52:35.417 回答