如何在Python中解析和打印CSV数据字段
我正在处理一个应用程序,它将文本导出为CSV格式的数据。这个文本被分成了几个部分,每个部分之间是通过换行符来分开的。我一直在尝试使用Python的CSV模块来恢复这些文本。
这是一个文本的例子:
{"This is an example", "of what I what I have to deal with. ", "Please pick up th following:", "eggs", "milk", "Thanks for picking groceries up for me"}
我想知道最好的方法是什么,来像这样读取这个文本:
This is an example of what I have to deal with. Please pick up the following: eggs milk Thanks for picking up the groceries for me
我尝试了很多方法,但都不太对劲。
这是我目前的做法:
import csv
import xlrd
book = xlrd.open_workbook("book1.xls")
sh = book.sheet_by_index(0)
cat = 'Mister Peanuts'
for r in range(sh.nrows)[0:]:
cat_name = sh.cell_value(rowx=r, colx=1)
cat_behavior = sh.cell_value(rowx=r, colx=5)
if sh.cell_value(rowx=r, colx=1) == cat :
csv_reader = csv.reader( ([ cat_behavior ]), delimiter=',')
for row in csv_reader:
for item in row:
item = item.strip()
print(item)
pass
pass
所以,实际上返回的关于cat_behavior的单元格值是这样的:
['{"Mister Peanut spent 3.2 hours with {bojangles} fighting', ' "', ' "litter box was cleaned, sanitized and replaced "', ' " Food was replensished - with the best food possible"', ' ', ' "technician - don johnson performed all tasks"}']
我现在正在尝试将上面的内容通过csv.reader处理一下,以清理数据并将其打印到文本文件中。我现在想让(item)看起来正常。
4 个回答
>>> s
'{"This is an example", "of what I what I have to deal with. ", "Please pick up th following:", "eggs", "milk", "Thanks for picking groceries up for me"}'
>>> print s.replace(",","\n").replace("{","").replace("}","").replace('"',"")
This is an example
of what I what I have to deal with.
Please pick up th following:
eggs
milk
Thanks for picking groceries up for me
>>> open("output.csv","w").write( s.replace(",","\n").replace("{","").replace("}","").replace('"',"") )
当然可以!请把你想翻译的内容发给我,我会帮你把它变得更容易理解。
import csv
with open('test') as f:
for row in csv.reader(f):
for item in row:
item=item.strip('{} "')
print(item)
strip 方法可以把字符串 item
左边或右边的指定字符去掉。
请先解释一下你手头有什么。
x = {"This is an example", ......., "Thanks for picking groceries up for me"}
这看起来像是一个集合。然后你把 [x]
作为 csv.reader 的第一个参数传入!! 这样是行不通的:
[Python 2.7]
>>> import csv
>>> x = {"foo", "bar", "baz"}
>>> rdr = csv.reader([x]) # comma is the default delimiter
>>> list(rdr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected string or Unicode object, set found
>>>
你说“一个将文本导出为 CSV 类型数据的应用程序”——“导出”是什么意思呢?如果是指“写入文件”,请你(如果你无法理解网上到处都有的例子)给我们提供一个文件的内容让我们看看。如果是指“方法/函数返回一个 Python 对象”,请执行 print(repr(python_object))
并把打印的结果复制粘贴到你的问题中。
你对这个应用程序输出的文档有什么了解吗?
更新,在评论和问题编辑后:
你说单元格的值“返回”的是:
['{"Mister Peanut spent 3.2 hours with {bojangles} fighting', ' "', ' "litter box was cleaned, sanitized and replaced "', ' " Food was replensished - with the best food possible"', ' ', ' "technician - don johnson performed all tasks"}']
这看起来像是你在将实际数据通过 CSV 处理后打印的结果,而不是通过 xlrd 提取的原始值,后者肯定不会是一个列表;它应该是一个单一的 Unicode 对象。
如果你之前没看到:请先解释一下你手头有什么开始的。
你觉得能做到以下这些吗:
(1) 请执行 print(repr(cat_behavior))
并把打印的结果复制粘贴到你的问题中。
(2) 说说你对创建 Excel 文件的应用程序有什么文档。