我的代码有问题:
import csv
with open('gios-pjp-data.csv', 'r') as data:
l = []
reader = csv.reader(data, delimiter=';')
next(reader)
next(reader) # I need to skip 2 lines here and dont know how to do it in other way
l.append(# here is my problem that I will describe below)
所以这个文件包含大约 350 行 4 列, 每个都是这样构建的:
Date ; float number ; float number ; float number
类似这样的事情:
2017-01-01;56.7;167.2;236.9
现在,我不知道如何构建一个函数,在其值 >200 的情况下将第一个 float 和第三个 float 附加到列表中。 您有什么建议吗?
最佳答案
如果文件中没有太多项目,请列出推导式。
l = [x[1], x[3] for x in reader if x[1] > 200]
或者如果您有大量条目,则可以使用类似的函数来生成每一行。
def getitems():
for x in reader:
if x[1] > 200:
yield x[1], x[3]
l = getitems() # this is now an iterator, more memory efficient.
l = list(l) # now its a list
关于python - 使用 '' 与 open、reader '' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48355493/