如何在Python中解析和打印CSV数据中的字段
我正在处理一个将文本导出为 CSV 类型数据的应用程序。文本被分成了几个很难返回的字段。我一直在尝试使用 pythons 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"}
What is the best way to read this output 这个文本如下:
这是一个例子 我必须处理的事情。 请选择以下内容: 鸡蛋 牛奶 谢谢你帮我买菜
我尝试了很多方法,但都不是很正确。
这是我到目前为止正在做的事情:
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 运行以清理它并将其打印到文本文件。我现在正在努力使(项目)看起来正常。
I'm dealing with an application that exports text as as CSV type data. The text is broken up into fields where there was a hard return. I have been trying to use pythons CSV to restore the text.
This is an example of the text:
{"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"}
What is the best way to read this output this text like so:
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
I have tried a number of ways that just haven't been quite right.
Here is what I am doing so far:
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
So, the actual cell value that is returned for cat_behavior is the following:
['{"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"}']
I am now trying to take the above and run in through csv.reader to sanitize it and print it to a text file. I am now trying to make the (item) look normal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
strip 方法 从左端或右端删除指定的字符字符串
项目
。The strip method removes the specified characters from the left or right end of the string
item
.请解释一下您必须从什么开始。
这看起来像一个集合。然后你将
[x]
作为 csv.reader 的第一个参数传递!这是行不通的:您说“将文本导出为 CSV 类型数据的应用程序”——“导出”是什么意思?如果它的意思是“写入文件”,请(如果您无法遵循网络上遍布的示例)向我们提供文件转储以供查看。如果这意味着“方法/函数返回一个 python 对象”,请执行
print(repr(python_object))
并使用打印输出的复制/粘贴来更新您的问题。您有哪些有关应用程序输出的文档?
编辑评论和问题后更新:
您说“返回”的单元格值为:
['{"花生先生与 {bojangles} 战斗了 3.2 小时', ' "', ' "垃圾箱是清洁、消毒和更换“','“食物已补充 - 尽可能提供最好的食物”','','“技术员 - 唐约翰逊执行了所有任务”}']
这看起来像您在传递实际数据后打印的内容CSV mangle,而不是 xlrd 提取的原始值,它当然不会是列表;它将是一个单一的 unicode 对象。
如果您之前没有读过:请解释一下您必须开始的内容。
您认为可以执行以下操作吗:
(1) 请执行
print(repr(cat_behavior))
并使用打印输出的副本/粘贴来更新您的问题。(2) 说明您拥有有关创建 Excel 文件的应用程序的哪些文档。
Please explain what you have got to start with.
That looks like a set. Then you pass
[x]
as the first arg of csv.reader!! That doesn't work:You say "application that exports text as as CSV type data" -- what does "exports" mean? If it means "writes to a file", please (if you can't follow the examples dotted all over the web) give us a dump of the file to look at. If it means "method/function returns a python object", please do
print(repr(python_object))
and update your question with a copy/paste of the print output.What documentation about the application output do you have?
Update after comments and question edited:
You say that the cell value "returned" was:
['{"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"}']
This looks like what you printed after passing the ACTUAL data through the CSV mangle, not the raw value extracted by xlrd, which certainly won't be a list; it would be a single unicode object.
In case you didn't read it before: Please explain what you have got to start with.
Do you think it possible to do these:
(1) please do
print(repr(cat_behavior))
and update your question with a copy/paste of the print output.(2) say what documentation you have about the application that creates the Excel file.
您需要查看
csv.writer
将数据导出到 csv,而不是csv.reader
。编辑:问题的正文和标题冲突。您使用
csv.reader
是正确的。您可以在 for 循环中使用print
来实现您想要的结果。You will need to look into
csv.writer
to export data to csv, rather thancsv.reader
.EDIT: The body and title of question conflict. You're right about using
csv.reader
.You can useprint
in a for loop to achieve the result you are after.