[英]How to transform a Python tuple to a .csv file?
我想將Python元組轉換為.csv
文件。 假設我有一個retrive()函數,當我用pprint
打印它時,它看起來像這樣:
test = tuple(retrive(directory))
pprint(test, width=1)
然后:
("opinion_1.txt, I am an amateur photographer and own three DSLR c.... purchase",
"opinion_2.txt, This my second Sony Digital Came.... good camera for a good price!',
'opinion_3.txt, \'I ordered this camera with high hopes after couldn\\\'t find.\'')
因此,我嘗試使用csv
模塊:
with open('/Users/user/Downloads/output.csv','w') as out:
csv_out=csv.writer(out)
csv_out.writerow(['id','content'])
for row in test:
csv_out.writerow(row)
問題是我得到一個奇怪的輸出,看起來像這樣:
id,content
o,p,i,n,i,o,n,_,1,.,t,x,t,",", ,I, ,a,m, ,a,n, ,a,m,a,t,e,u,r, ,p,h,o,t,o,g,r,a,p,h,e,r, ,a,n,d, ,o,w,n, ,t,h,r,e,e, ,D,S,L,R, ,c,a,m,e,r,a,s, ,w,i,t,h, ,a, ,s,e,l,e,c,t,i,o,n, ,o,f, ,l,e,n,s,e,s,., ,H,o,w,e,v,e,r, ,t,h,a,t, ,c,o,l,l,e,c,t,i,o,n,
我如何獲得這樣的東西:
opinion_1.txt,I am an amateur photographer and own three DSLR c.... purchase
opinion_2.txt,This my second Sony Digital Came.... good camera for a good price!
opinion_3.txt,I ordered this camera with high hopes after couldn\\\'t find.
CSV嘗試遍歷從元組傳遞的字符串。 將您的代碼更改為:
for row in test:
csv_out.writerow(row.split(', ', 1))
這意味着您通過第一次出現', '
拆分元組中的每個字符串。 它為每一行產生兩個元素,這是csv writer所需要的。
如果您需要Pandas
解決方案,請使用DataFrame constructor
和to_csv
:
import pandas as pd
df = pd.DataFrame([ x.split(',') for x in test ])
df.columns = ["id","content"]
print df
# id content
#0 opinion_1.txt I am an amateur photographer and own three DS...
#1 opinion_2.txt This my second Sony Digital Came.... good cam...
#2 opinion_3.txt 'I ordered this camera with high hopes after ...
#for testing
#print df.to_csv(index=False)
df.to_csv("/Users/user/Downloads/output.csv", index=False)
#id,content
#opinion_1.txt, I am an amateur photographer and own three DSLR c.... purchase
#opinion_2.txt, This my second Sony Digital Came.... good camera for a good price!
#opinion_3.txt, 'I ordered this camera with hig
如果有多個,
你可以使用split
由第一次出現,
:
import pandas as pd
test = ("opinion_1.txt,a","opinion_2.txt,b","opinion_3.txt,c", "opinion_3.txt,b,c,k")
print test
print [ x.split(',', 1) for x in test ]
[['opinion_1.txt', 'a'],
['opinion_2.txt', 'b'],
['opinion_3.txt', 'c'],
['opinion_3.txt', 'b,c,k']]
df = pd.DataFrame([ x.split(',', 1) for x in test ])
df.columns = ["id","content"]
print df
id content
0 opinion_1.txt a
1 opinion_2.txt b
2 opinion_3.txt c
3 opinion_3.txt b,c,k
print df.to_csv(index=False)
id,content
opinion_1.txt,a
opinion_2.txt,b
opinion_3.txt,c
opinion_3.txt,"b,c,k"
如果您的其中一個句子具有多個逗號,則您的分析將被破壞:
s = "opinion_4.txt, Oh my, what happens with really, really long sentences?"
>>> s.split(", ")
['opinion_4.txt',
'Oh my',
'what happens with really',
'really long sentences?']
更好的方法是找到第一個逗號,然后在該位置使用切片將句子拆分:
for line in text:
comma_idx = line.find(', ')
csvout.writerow(line[:comma_idx], line[comma_idx+2:])
對於上面的句子,這將導致以下結果:
('opinion_4.txt', 'Oh my, what happens with really, really long sentences?')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.