【问题标题】:Python For loop reads only half of filePython For循环只读取文件的一半
【发布时间】:2018-03-22 04:20:19
【问题描述】:

所以我试图遍历一个 .csv 文件并根据它进行一些计算,我的问题是该文件有 10001 行长,而当我的程序执行时,它似乎只读取了其中的 5001 行。读取数据时我做错了什么,或者我遇到了内存限制或其他某种限制?计算结果很好,但在某些情况下与预期结果有偏差,因此我相信丢失的一半数据可以解决这个问题。

fileName = 'normal.csv' #input("Enter a file name: ").strip()
file = open(fileName, 'r') #open the file for reading
header = file.readline().strip().split(',') #Get the header line
data = [] #Initialise the dataset
for index in range(len(header)):
    data.append([])
for yy in file:
    ln = file.readline().strip().split(',') #Store the line
    for xx in range(len(data)):
        data[xx].append(float(ln[xx]))

这是一些示例输出,尚未完全格式化,但最终会:

"""The file normal.csv contains 3 columns and 5000 records.
         Column Heading   |        Mean        |     Std. Dev.      
      --------------------+--------------------+--------------------
      Width [mm]|999.9797|2.5273
      Height [mm]|499.9662|1.6889
      Thickness [mm]|12.0000|0.1869"""

由于这是家庭作业,我希望您尝试保持回复有帮助,但不要直接解决问题,谢谢。

【问题讨论】:

  • AFAICT,您在一次迭代中读取 2 行。 “yy”已经包含了一行,调用“file.readline”会将你移到下一行。你应该直接处理“yy”的内容而不调用readline。

标签: python-3.x file csv


【解决方案1】:

那是因为您要求 Python 读取两个不同位置的行:

for yy in file:

ln = file.readline().strip().split(',') #Store the line

yy 已经是文件中的一行,但你忽略了它;对文件对象的迭代会从文件中产生行。然后,您使用file.readline() 阅读另一行。

如果你使用迭代,也不要使用readline(),只需使用yy

for yy in file:
    ln = yy.strip().split(',') #Store the line

但是,您正在重新发明 CSV 阅读轮。只需改用csv module

您可以将 CSV 文件中的所有数据读取到每列的列表中,其中包含一些 zip() function trickery

import csv

with open(fileName, 'r', newline='') as csvfile:
    reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)  # convert to float
    header = next(reader, None)   # read one row, the header, or None
    data = list(zip(*reader))  # transpose rows to columns

【讨论】:

  • 做到了。谢谢。将 ln = file.readline().strip().split(',') 更改为 ln = yy.strip().split(',') 就像魅力一样。
  • 至于重新发明轮子,这是大多数 comp-sci 课程所需要的,但无论如何感谢您的提醒。
猜你喜欢
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2018-07-16
相关资源
最近更新 更多