For循环转储整个文件,而不是第一个lin

2024-06-08 22:14:28 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的for循环转储整个文件(.txt)而不是第一行是什么?你知道吗

For循环:

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

Tags: orenglishalhealthclinicalnccmentaldepression
2条回答

如果您只需要第一行(例如获取标题),那么这应该可以做到:

with open('file.txt', 'rb') as f:
    print f.readline().split()

假设f被实例化为open("file.txt", "r"),那么您就是在文件上迭代的普通Jane。你知道吗

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

with(open("file.txt", "r")) as f:
    li = f.readlines()
    for line in li[1:]:
        print line.split()

相关问题 更多 >