首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >For循环转储整个文件,而不是第一行

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

Stack Overflow用户
提问于 2013-06-28 21:41:20
回答 2查看 150关注 0票数 0

下面的for循环会转储整个文件(.txt)而不是第一行?

For循环:

代码语言:javascript
复制
counter = 0

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

文件格式:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2013-06-28 21:44:44

假设f被实例化为open("file.txt", "r"),那么您只需简单地迭代文件即可。

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

代码语言:javascript
复制
with(open("file.txt", "r")) as f:
    li = f.readlines()
    for line in li[1:]:
        print line.split()
票数 2
EN

Stack Overflow用户

发布于 2013-06-28 21:52:35

如果您想要的只是第一行(例如获取标题),那么下面的代码应该可以做到这一点:

代码语言:javascript
复制
with open('file.txt', 'rb') as f:
    print f.readline().split()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17366074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档