读取csv文件-单个列表中的每个单个字符 [英] csv file read - every single character in one single list
问题描述
我是python的新手,可能真的需要一些帮助(到目前为止,我还没有找到任何可以帮助我的东西).
I am rather new to python and could really need some help (I did not find anything that helped me by now).
我想将csv文件读取为list
,但是不幸的是我的输出与预期的不一样.而不是像这样的列表:
I want to read a csv-file to a list
, but unfortunately my output is not as expected. Instead of having a list like:
[[Weiz;61744],[Deutschlandsberg;5645]]
我有一个看起来像这样的列表:
I have a list that looks like this:
[['W'],['e'],['i'], etc.]
我的代码如下:
def readCSV(file):
for row in open(file,"r+"):
ftpstream = urllib.request.urlopen(row)
csvFile = csv.reader(ftpstream.read().decode('latin-1'))
data = [row for row in csvFile]
for row in data:
print(row)
有人可以告诉我为什么它不起作用吗?我现在真的很挣扎...
Can anybody please tell me why it is not working? I am really struggling right now...
推荐答案
函数csv.reader
希望接收行列表,而不是单个字符串(例如ftpstream.read().decode('latin-1')
返回的字符串).如果您替换:
The function csv.reader
expects to receive a list of lines, not a single string (such as would be returned by ftpstream.read().decode('latin-1')
). If you replace:
csvFile = csv.reader(ftpstream.read().decode('latin-1'))
使用
csvFile = csv.reader(ftpstream.read().decode('latin-1').split('\n'))
我相信它会按您期望的那样工作.
I believe it will work as you expect.
这篇关于读取csv文件-单个列表中的每个单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!