2010-10-05 30 views
2

我正在處理一個將文本導出爲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進行清理並將其打印到文本文件中。我現在試圖使(項目)看起來很正常。

+0

替換'在範圍(sh.nrows)R [0:]:''與對於r在sh.nrows:' – 2010-10-05 19:49:53

回答

0

您需要查看csv.writer才能將數據導出到csv,而不是csv.reader

編輯:問題衝突的正文和標題。您正確使用csv.reader。您可以在for循環中使用print來實現您所追求的結果。

+0

謝謝,添。我現在非常接近使用這種方法! – SPORKEATER 2010-10-05 05:13:57

1
import csv 
with open('test') as f: 
    for row in csv.reader(f): 
     for item in row: 
      item=item.strip('{} "') 
      print(item) 

strip method去除從字符串item的左或右端的指定的字符。

+0

非常感謝。這確實有助於我前進。問題在於CSV可能實際上具有波形括號和引用,這可能是一個問題。 – SPORKEATER 2010-10-05 03:55:28

1

請解釋一下你有什麼開始。

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))並用複製/粘貼打印輸出來更新您的問題。

你有什麼關於應用程序輸出的文檔?

更新意見和問題後編輯:

你說的那個單元格值 「返回」 是:

['{「花生先生花了3。2個小時與{bojangles}戰鬥','''''''垃圾箱被清洗,消毒並且替換'',''食物被補充 - 與最佳的食物可能'','','「技術員 - 唐約翰遜執行了所有任務「}」]

這看起來像是在通過CSV mangle傳遞ACTUAL數據之後打印的內容,而不是xlrd提取的原始值,它肯定不會是一個列表;它將是一個unicode對象。

如果你沒看過前:請解釋什麼,你得開始

你認爲它可能噸。做這些:

(1)請做print(repr(cat_behavior))並更新您的問題與複製/粘貼打印輸出。 (2)說明你有哪些關於創建Excel文件的應用程序的文檔。

+0

我正在使用您的xlrd從電子表格中提取單元格,然後將這些單元格值打印到某人可以真正閱讀的文本文件中。我需要打印的一個單元格包含CSV數據。所以,我試圖在將文本打印到文本文件之前使其可讀。 – SPORKEATER 2010-10-05 03:59:03

+0

我已經編輯過來顯示我實際上是從單元格中的一段文字做的。 – SPORKEATER 2010-10-05 04:42:15

0
>>> 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('"',"")) 
+0

謝謝。我是python的新手,但如果嵌套引號或大括號不會造成問題嗎?這是一個不幸的可能性。 – SPORKEATER 2010-10-05 03:57:34

+0

如果你有嵌套的東西,那麼不要使用這種方法。 – ghostdog74 2010-10-05 04:00:54

+0

CSV文件中不應嵌套過多。 – 2010-10-05 19:47:51