How to transform a Python tuple to a .csv file? -


i transform python tuple .csv file. let's have retrive() function , when print pprint looks this:

test = tuple(retrive(directory)) pprint(test, width=1) 

then:

("opinion_1.txt, amateur photographer , own 3 dslr c.... purchase",  "opinion_2.txt, second sony digital came.... camera price!',  'opinion_3.txt, \'i ordered camera high hopes after  couldn\\\'t find.\'') 

so, tried csv module:

with open('/users/user/downloads/output.csv','w') out:     csv_out=csv.writer(out)     csv_out.writerow(['id','content'])     row in test:         csv_out.writerow(row) 

the problem weird output looks this:

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,  

how can this:

opinion_1.txt,i amateur photographer , own 3 dslr c.... purchase opinion_2.txt,this second sony digital came.... camera price! opinion_3.txt,i ordered camera high hopes after  couldn\\\'t find. 

csv trying iterate on string pass tuple. change code to:

for row in test:     csv_out.writerow(row.split(', ', 1)) 

it means split each string in tuple first occurrence of ', '. produces 2 elements each row , csv writer need is.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -