我正在處理一個將文本導出爲CSV類型數據的應用程序。文本被分解成硬迴歸的字段。我一直在嘗試使用python的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進行清理並將其打印到文本文件中。我現在試圖使(項目)看起來很正常。
替換'在範圍(sh.nrows)R [0:]:''與對於r在sh.nrows:' – 2010-10-05 19:49:53