python 读取csv文件没有for
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2243655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading csv file without for
提问by Abruzzo Forte e Gentile
I need to read a CSV file in python.
我需要在 python 中读取一个 CSV 文件。
Since for last row I receive a 'NULL byte' error I would like to avoid using for keyword but the while.
由于对于最后一行,我收到一个“NULL 字节”错误,我想避免使用 for 关键字,但使用 while。
Do you know how to do that?
你知道怎么做吗?
reader = csv.reader( file ) for row in reader # I have an error at this line # do whatever with row
I want to substitute the for-loop with a while-loop so that I can check if the row is NULL or not.
我想用 while 循环替换 for 循环,以便我可以检查该行是否为 NULL。
What is the function for reading a single row in the CSV module? Thanks
CSV模块中读取单行的功能是什么?谢谢
Thanks
谢谢
p.S. below the traceback
回溯下方的 pS
Traceback (most recent call last): File "FetchNeuro_TodayTrades.py", line 189, in for row in reader: _csv.Error: line contains NULL byte
回答by Pedro Ghilardi
Maybe you could catch the exception raised by the CSV reader. Something like this:
也许您可以捕获 CSV 阅读器引发的异常。像这样的东西:
filename = "my.csv"
reader = csv.reader(open(filename))
try:
for row in reader:
print 'Row read with success!', row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
Or you could use next()
:
或者你可以使用next()
:
while True:
try:
print reader.next()
except csv.Error:
print "Error"
except StopIteration:
print "Iteration End"
break
回答by John Machin
You need (always) to say EXACTLY what is the error message that you got. Please edit your question.
您需要(始终)准确说出您收到的错误消息是什么。请编辑您的问题。
Probably this:
大概是这样的:
>>> import csv; csv.reader("\x00").next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_csv.Error: line contains NULL byte
>>>
The csv module is not 8-bit clean; see the docs: """Also, there are currently some issues regarding ASCII NUL characters."""
csv 模块不是 8 位干净的;请参阅文档:"""此外,目前存在一些有关 ASCII NUL 字符的问题。"""
The error message is itself in error: it should be "NUL", not "NULL" :-(
错误消息本身是错误的:它应该是“NUL”,而不是“NULL”:-(
If the last line in the file is empty, you won't get an exception, you'll merely get row == []
.
如果文件的最后一行是空的,你不会得到异常,你只会得到row == []
.
Assuming the problem is one or more NULs in your file(s), you'll need to (1) speak earnestly to the creator(s) of your file(s) (2) failing that, read the whole file in (mode="rb"), strip out the NUL(s), and feed fixed_text.splitlines()
to the csv reader.
假设问题是您的文件中有一个或多个 NUL,您需要 (1) 认真地与文件的创建者交谈 (2) 失败,在 (mode ="rb"),去掉 NUL(s),然后提供fixed_text.splitlines()
给 csv 阅读器。
回答by Dave Everitt
The Django community has addressed Python CSV import issues, so it might be worth searching for CSV importthere, or posting a question. Also, you could edit the offending line directly in the CSV file before trying the import.
Django 社区已经解决了 Python CSV 导入问题,因此可能值得在那里搜索 CSV 导入,或发布问题。此外,您可以在尝试导入之前直接在 CSV 文件中编辑违规行。
回答by John Fouhy
You could try cleaning the file as you read it:
您可以在阅读文件时尝试清理文件:
def nonull(stream):
for line in stream:
yield line.replace('\x00', '')
f = open(filename)
reader = csv.reader(nonull(f))
Assuming, of course, that simply ignoring NULL characters will work for you!
当然,假设简单地忽略 NULL 字符对您有用!
回答by dalloliogm
If your problem is specific to the last line being empty, you can use numpy.genfromtxt (or the old matplotlib.mlab.csv2rec)
如果您的问题特定于最后一行为空,您可以使用 numpy.genfromtxt(或旧的 matplotlib.mlab.csv2rec)
$: cat >csv_file.txt
foo,bar,baz
yes,no,0
x,y,z
$:
$: ipython
>>> from numpy import genfromtxt
>>> genfromtxt("csv_file.txt", dtype=None, delimiter=',')
array([['foo', 'bar', 'baz'],
['yes', 'no', '0'],
['x', 'y', 'z']],
dtype='|S3')
回答by ghostdog74
not really sure what you mean, but you can always check for existence with if
不太确定你的意思,但你总是可以用 if 检查是否存在
>>> reader = csv.reader("file")
>>> for r in reader:
... if r: print r
...
if this is not what you want, you should describe your problem more clearly by showing examples of things that doesn't work for you, including sample file format and desired output you want.
如果这不是您想要的,您应该通过显示不适合您的示例更清楚地描述您的问题,包括示例文件格式和您想要的所需输出。
回答by terry
Process the initial csv
file and replace the Nul '\0'
with blank, and then you can read it.
The actual code looks like this:
处理初始csv
文件并将其替换为Nul '\0'
空白,然后您就可以阅读它了。实际代码如下所示:
data_initial = open(csv_file, "rU")
reader = csv.reader((line.replace('import csv
FH = open('data.csv','wb')
line1 = [97,44,98,44,99,10]
line2 = [100,44,101,44,102,10]
for n in line1 + line2:
FH.write(chr(n))
FH.write(chr(0))
FH.close()
FH = open('data.csv')
reader = csv.reader(FH)
for line in reader:
if '##代码##' in line: continue
if not line: continue
print line
$ python script.py
['a', 'b', 'c']
['d', 'e', 'f']
Traceback (most recent call last):
File "script.py", line 11, in <module>
for line in reader:
_csv.Error: line contains NULL byte
','') for line in data_initial))
It works for me.
这个对我有用。
And the original answer is here:csv-contain null byte
原始答案在这里:csv-contain null byte
回答by telliott99
I don't have an answer, but I can confirm the problem, and that most answers posted don't work. You cannot catch this exception. You cannot test for if line
. Maybe you could check for the NULL byte directly, but I'm not swift enough to do that... If it is always on the last line, you could of course skip that.
我没有答案,但我可以确认这个问题,并且发布的大多数答案都不起作用。您无法捕获此异常。您无法测试if line
. 也许你可以直接检查 NULL 字节,但我做的不够快......如果它总是在最后一行,你当然可以跳过它。