简体   繁体   English

跳过前 48 行后,逐行读取 python 的 Pandas

[英]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.

相关问题 python文件逐行读取并检查条件,如果不正确,请等到其正确而不跳过该行 - python file reading line by line and check for condition and if it's not true wait until its true without skipping the line 标题中的Python中的Readlines方法跳过文件的第一行 - Readlines method in Python skipping the first line in a file after heading 跳过csv.file中的第一行,当它是一个字符串时,使用python - Skipping first line in csv.file, when it's a string, with python python中的正则表达式跳过前两行的字符 - Regular expression in python is skipping first 2 characters of line python mapreduce-在mapper中跳过.csv的第一行 - python mapreduce - Skipping the first line of the .csv in mapper 为什么 python 跳过 txt 文件的第一行 - Why is python skipping the first line of a txt file Python input() 函数在第一个输入上跳过一行 - Python input() function is skipping a line on the first input Readline 正在跳过第一行 - Readline is skipping the first line 在多线程中使用 Python 逐行读取文件是只读取第一行 - Reading file line by line with Python in multithreading is reading only the first line 读入python后从文本文件中删除第一个标题行 - Removing the first title line from a text file after reading into python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM