我想在csv中写一些列表的项目,这些项目在引号内并且在它们之间有逗号。如何编写它们以使项目不会因逗号而分开?我查看了许多类似的问题,但没有找到解决我问题的具体方法。
['Saks, Lord & Taylor data breach may affect 5 million customers', (u'By Mike Murphy', u'April 1, 2018, 5:24 p.m. EST')]
['Why The China Hustle is a finance documentary all U.S. investors need to see', (u'By MarketWatch', u'NewsWatch')]
['Trump says no deal for Dreamers, again threatens to end Nafta in Easter tweets', (u'By Mike Murphy', u'April 1, 2018, 4:49 p.m. EST')]
预期答案:
first column = 'Saks, Lord & Taylor data breach may affect 5 million customers'
'Why The China Hustle is a finance documentary all U.S. investors need to see'
'Trump says no deal for Dreamers, again threatens to end Nafta in Easter tweets'
second column = 'By Mike Murphy'
'By MarketWatch'
'By Mike Murphy'
third column = 'April 1, 2018, 5:24 p.m. EST'
'NewsWatch'
'April 1, 2018, 4:49 p.m. EST'
答案 0 :(得分:2)
它很简单:
x=[['Saks, Lord & Taylor data breach may affect 5 million customers', (u'By Mike Murphy', u'April 1, 2018, 5:24 p.m. EST')],['Why The China Hustle is a finance documentary all U.S. investors need to see', (u'By MarketWatch', u'NewsWatch')],['Trump says no deal for Dreamers, again threatens to end Nafta in Easter tweets', (u'By Mike Murphy', u'April 1, 2018, 4:49 p.m. EST')]]
import csv
with open('bla.csv','w') as fd:
writer=csv.writer(open('bla.csv','w'))
writer.writerows(x)
注意"为什么选择中国......"未在csv
中引用,因为它不包含任何逗号。如果您想引用它,可以在quoting=QUOTE_ALL
构造函数中使用writer
。
答案 1 :(得分:1)
这个怎么样?
next_field<-function(stream) {
p<-seek(stream)
d<-readChar(stream,1)
seek(stream,p)
if(d=="\"")
field<-scan(stream,"",1,sep=",",quote="\"",blank=FALSE)
else
field<-scan(stream,"",1,sep=",",quote="",blank=FALSE)
return(field)
}
s<-file("C:/your_path_here/sample.csv",open="rt")
header<-readLines(s,1)
header<-scan(what="",text=header,sep=",")
line<-replicate(length(header),next_field(s))
setNames(as.data.frame(lapply(line,type.convert)),header)