如何编写带引号的csv? [英] How to write a csv with quotes?

查看:58
本文介绍了如何编写带引号的csv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python,也正在使用csv解析脚本,但是我无法以CSV格式将带引号的新文件写为输入文件,我以这种方式访问​​该文件:

I'm working with python, and also i'm working on a csv parse script, but i cannot write as CSV the new file with quotes as the input file, i access to the file in this way:

def read_file(self):
    with open(self.file) as f:
        parse_data=[row  for row in csv.reader(f, delimiter=',',quotechar=('\"'), skipinitialspace=True)]
        parse_data=self.organize_file(parse_data)
        return parse_data

当我尝试编写新的CSV文件以存储新文件时,我执行以下命令:

and when i try to write a new CSV file to store the new file i execute this:

with open("output.txt","wb") as m:
    writer=csv.writer(m,delimiter=',',quotechar=('\n"'))
    writer.writerows(meh)

以meh作为变量,该变量具有要在CSV文件中写入的列表,但是当我打开output.txt文件时,我看不到字段中的引号,就像原始的

with meh as the variable who has the lists to write on the CSV file, but when i open the output.txt file i cannot see the quotes on the fields, like the original

有人可以帮助我了解我的代码有什么问题吗,或者请帮助我修改输出以包含所有引号

can somebody help me to realize what's wrong with my code, or pleas help me to modify the output to have all the quotes

谢谢!

推荐答案

尝试将csv.writer行修改为:

Try ammending the csv.writer line to:

writer=csv.writer(m,delimiter=',',quotechar=('\n"'),quoting=csv.QUOTE_ALL)

有关其他引用选项,请参见此处.

See here for other quoting options.

这篇关于如何编写带引号的csv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆