[英]Reading line by line with python's Pandas after skipping first 48 rows
The title is fairly explanatory.标题是相当解释性的。
I have a long CSV file that I would like to read line by line with the following code:我有一个很长的 CSV 文件,我想使用以下代码逐行读取:
lines = []
for line in pd.read_csv(file, chunksize = 1, header = None):
lines.append(line.iloc[0 0])
print(lines)
I'd like to skip the first 48 rows.我想跳过前 48 行。 At first it seemed simple enough and I thought all I needed to do was change my read function to:
起初它看起来很简单,我认为我需要做的就是将我的阅读 function 更改为:
pd.read_csv(file,chunksize = 1, header = None, skiprows = 48):
Sadly, this seems to produce the effect of skipping 48 rows every single loops.可悲的是,这似乎产生了每个循环跳过 48 行的效果。 Not a great outcome.
不是很好的结果。
How can I read line by line which is effectively reading this file while simultaneously skipping the first 48 rows of this long, irregular file?如何逐行读取有效读取此文件的同时跳过这个长而不规则文件的前 48 行?
You could set skiprows to a variable that gets reset after its first execution.您可以将 skiprows 设置为在第一次执行后重置的变量。
lines = []
row_skip = 48
for line in pd.read_csv(file, chunksize = 1, header = None,skiprows=row_skip):
lines.append(line.iloc[0,0])
if row_skip:
row_skip = None
print(lines)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.