[英]How to skip blank lines with read_fwf in pandas?
我在Python pandas 0.19.2中使用pandas.read_fwf()
函数来读取具有以下内容的文件fwf.txt
:
# Column1 Column2
123 abc
456 def
#
#
我的代码如下:
import pandas as pd
file_path = "fwf.txt"
widths = [len("# Column1"), len(" Column2")]
names = ["Column1", "Column2"]
data = pd.read_fwf(filepath_or_buffer=file_path, widths=widths,
names=names, skip_blank_lines=True, comment="#")
打印的数据框如下:
Column1 Column2
0 123.0 abc
1 NaN NaN
2 456.0 def
3 NaN NaN
看起来像skip_blank_lines=True
参数被忽略,因为数据帧包含NaN。
什么应该是pandas.read_fwf()
参数的有效组合,以确保跳过空行?
import io
import pandas as pd
file_path = "fwf.txt"
widths = [len("# Column1 "), len("Column2")]
names = ["Column1", "Column2"]
class FileLike(io.TextIOBase):
def __init__(self, iterable):
self.iterable = iterable
def readline(self):
return next(self.iterable)
with open(file_path, 'r') as f:
lines = (line for line in f if line.strip())
data = pd.read_fwf(FileLike(lines), widths=widths, names=names,
comment='#')
print(data)
版画
Column1 Column2
0 123 abc
1 456 def
with open(file_path, 'r') as f:
lines = (line for line in f if line.strip())
定义一个生成器表达式(即一个可迭代的),它从文件中产生一行,并删除空白行。
pd.read_fwf
函数可以接受TextIOBase
对象。 您可以TextIOBase
子类,以便其readline
方法从iterable返回行:
class FileLike(io.TextIOBase):
def __init__(self, iterable):
self.iterable = iterable
def readline(self):
return next(self.iterable)
将这两者放在一起为您提供了一种在将文件传递给pd.read_fwf
之前操作/修改文件行的pd.read_fwf
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.