[英]Loading UTF-8 file in Python 3 using numpy.genfromtxt
[英]Locate the skipped rows in source file after using numpy genfromtxt python
我是关于python中genfromtxt
问题。 我使用以下代码:
syms = np.genfromtxt('final.csv', delimiter = ';', dtype=str, skip_header=1, invalid_raise=False)[:, 0:]
由于invalid_raise
,会跳过多个行。 有没有办法在源文件(csv)中找到跳过的行,以便从那里删除它们? 提前致谢!
试试这个(可能需要调试):
with open('final.csv') as f:
lines = f.readlines()
lens = [len(line.split(';')) for line in lines]
# should give the length of each line
len1 = lens[1] # the length of the first data line
errors = [i for i,n in enumerate(lens[1:] if n!=len1]
# should give line number of lines with length different from len1
我手边没有好的测试文件,所以不会尝试测试/调试它。 但逻辑应该是直截了当的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.