【问题标题】:How do i skip the first two lines from my text file? [duplicate]如何跳过文本文件的前两行? [复制]
【发布时间】:2020-03-14 22:33:06
【问题描述】:

这是我的代码

from collections import Counter 
counter = Counter()
with open('demo.txt') as f:
    for line in f:
        splits = line.split(';')
        change = float(splits[6])
        country = splits[1].strip()
        counter[country] += change
#Percentage Change By Countries"
print()
print ("Percentage Change By Countries")
for country, change_sum in counter.most_common():
    print(country, change_sum,"%")

这是文本文件“Demo.txt”

World Population Data 2019 from the United Nations
Rank; Country; 2018; 2019; % Share; Pop Change; % Change; Continent
1; China; 1427647786; 1433783686; 18.6; 6135900; 0.43; Asia
2; India; 1352642280; 1366417754; 17.7; 13775474; 1.02; Asia
3; United States of America; 327096265; 329064917; 4.27; 1968652; 0.60; North America
4; Indonesia; 267670543; 270625568; 3.51; 2955025; 1.10; Asia
5; Pakistan; 212228286; 216565318; 2.81; 4337032; 2.04; Asia
6; Brazil; 209469323; 211049527; 2.74; 1580204; 0.75; South America
7; Nigeria; 195874683; 200963599; 2.61; 5088916; 2.60; Africa
8; Bangladesh; 161376708; 163046161; 2.11; 1669453; 1.03; Asia
9; Russian Federation; 145734038; 145872256; 1.89; 138218; 0.09; Europe
10; Mexico; 126190788; 127575529; 1.65; 1384741; 1.10; North America

我尝试了 readlines() 但收到错误“超出范围”。如何跳过前两行?

【问题讨论】:

  • if x[0].isnumeric() and userInput in x.lower():
  • 为什么不去掉不必要的代码,并在问题中添加错误信息的 Traceback。

标签: python python-3.x


【解决方案1】:

def file_search():
    userInput = input('Enter a country: ').lower()
    result = []
    with open("json.txt", 'r') as f:
        for x in f:
            if userInput in x.lower():
                result.append(x.split(';'))
    for s in result:
        print(s[1] + "check:" + s[3])
file_search()
from collections import Counter 
counter = Counter()
with open('json.txt') as f:
    for i in range(0,2):
            next(f)

    for line in f:
        splits = line.split(';')
        change = float(splits[6])
        country = splits[1].strip()
        counter[country] += change
#Percentage Change By Countries"
print()
print ("Percentage Change By Countries")
for country, change_sum in counter.most_common():
    print(country, change_sum,"%")

【讨论】:

    【解决方案2】:

    要么使用@SayanipDutta 的评论,所以不要:

    if userInput in x.lower():
    

    你使用:

    if x[0].isnumeric() and userInput in x.lower():
    

    或者做:

    with open("demo.txt", 'r') as f:
    
            for x in f.readlines()[2:]:
              if userInput in x.lower():
                result.append(x.split(';'))
    

    【讨论】:

      【解决方案3】:

      如果你想跳过前 n 行,你可以在文件对象上调用next n 次:

      with open("demo.txt") as f:
          for _ in range(2):
              next(f)
          for line in f:
              ...
      

      此解决方案避免了调用f.readlines(),这将分配一个包含所有行的列表,然后您将其切片以分配另一个列表。

      【讨论】:

        【解决方案4】:

        this may help

        还有

         with open(file) as f:
            content = f.readlines()
            content = content[2:]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-07-14
          • 1970-01-01
          • 2018-09-10
          • 2016-05-06
          • 1970-01-01
          • 2019-06-13
          • 2011-02-17
          相关资源
          最近更新 更多