DictReader,无引号,选项卡式文件

发布于 2024-10-24 06:23:30 字数 506 浏览 1 评论 0原文

我有一个 csv 文件,如下所示: 请注意,没有引号,制表符 (\t) 是分隔符,标题和实际内容之间有一个空行。

Facility No     Testing No      Name    Age

252     2351    Jackrabbit, Jazz        15
345     257     Aardvark, Ethel 41

我想我已经尝试了几乎所有可能的想法和参数的组合,

f = open('/tmp/test', 'r')
csvFile = f.read()
reader = csv.DictReader(csvFile, delimiter='\t', quoting=csv.QUOTE_NONE)
print reader.fieldnames

打印的结果是:

['F']

如何将其转换为我可以解析并放入数据库的内容? 把它编入字典会很有帮助。

I have a csv file that looks like this:
Please note, there are no quotes, a tab (\t) is the delimiter, and there is a blank line between the header and the actual content.

Facility No     Testing No      Name    Age

252     2351    Jackrabbit, Jazz        15
345     257     Aardvark, Ethel 41

I think I've tried nearly every possible combination of ideas and parameters

f = open('/tmp/test', 'r')
csvFile = f.read()
reader = csv.DictReader(csvFile, delimiter='\t', quoting=csv.QUOTE_NONE)
print reader.fieldnames

the result of the print is:

['F']

How can I get this into something I can parse to put into a database?
Getting it into a dictionary would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱已欠费 2024-10-31 06:23:30

您的 csvFile 是什么?它是代表以“F”开头的文件名的字符串吗?

csv.DictReader 需要一个打开的文件对象,而不是文件名。

尝试:

with open(csvFile, 'rb') as f:
    reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    print reader.fieldnames

编辑

如果您的csvFile是包含整个数据的字符串,则必须将其转换为StringIO(因为csv 只能访问类似文件的对象,不能访问字符串)。

尝试:

from cStringIO import StringIO

# csvFile = 'Facility No\tTesting No\tName\tAge\n\n252\t2351\tJackrabbit, Jazz\t15\n345\t257\tAardvark, Ethel\t41\n'
reader = csv.DictReader(StringIO(csvFile), delimiter='\t', quoting=csv.QUOTE_NONE)
print reader.fieldnames

或者,如果您编辑的问题打开并读取文件:

with open('/tmp/test', 'rb') as f:
    reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    print reader.fieldnames

这对我有用。

What is your csvFile? Is it a string representing your filename starting with 'F'?

csv.DictReader needs an opened file object, not a filename.

Try:

with open(csvFile, 'rb') as f:
    reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    print reader.fieldnames

EDIT

If your csvFile is a string containing the whole data, you will have to convert it into a StringIO (because csv can access only file-like objects, not strings).

Try:

from cStringIO import StringIO

# csvFile = 'Facility No\tTesting No\tName\tAge\n\n252\t2351\tJackrabbit, Jazz\t15\n345\t257\tAardvark, Ethel\t41\n'
reader = csv.DictReader(StringIO(csvFile), delimiter='\t', quoting=csv.QUOTE_NONE)
print reader.fieldnames

Or, if your edited question opens and reads a file:

with open('/tmp/test', 'rb') as f:
    reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    print reader.fieldnames

This works for me.

扛刀软妹 2024-10-31 06:23:30

这可能对你有用,至少作为一个开始:


>>> import csv
>>> input = open('/tmp/csvtemp.csv')
>>> csvin = csv.reader(input, delimiter='\t')
>>> data = [row for row in csvin]
>>> header = data.pop(0)
>>> data.pop(0)  # skip blank line
[]
>>> for row in data:
...  rowdict = dict(zip(header, row))
...  print rowdict
... 
{'Age': '15', 'Testing No': '2351', 'Name': 'Jackrabbit, Jazz', 'Facility No': '252'}
{'Age': '41', 'Testing No': '257', 'Name': 'Aardvark, Ethel', 'Facility No': '345'}

this might work for you, at least as a start:


>>> import csv
>>> input = open('/tmp/csvtemp.csv')
>>> csvin = csv.reader(input, delimiter='\t')
>>> data = [row for row in csvin]
>>> header = data.pop(0)
>>> data.pop(0)  # skip blank line
[]
>>> for row in data:
...  rowdict = dict(zip(header, row))
...  print rowdict
... 
{'Age': '15', 'Testing No': '2351', 'Name': 'Jackrabbit, Jazz', 'Facility No': '252'}
{'Age': '41', 'Testing No': '257', 'Name': 'Aardvark, Ethel', 'Facility No': '345'}
枉心 2024-10-31 06:23:30

从评论中我了解到您通过获取数据urllib2response 是一个类似文件的对象;您可以将其直接传递给 csv.DictReader

response = urllib2.urlopen(URL)
reader = csv.DictReader(response, dialect=csv.excel_tab)

From the comments I understand that you get your data via urllib2. response is a file-like object; you could pass it directly to csv.DictReader:

response = urllib2.urlopen(URL)
reader = csv.DictReader(response, dialect=csv.excel_tab)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文