使用没有引号的python csv writer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23882024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:35:48  来源:igfitidea点击:

Using python csv writer without quotations

pythoncsvquotesdouble-quotes

提问by samsamara

I'm trying to write a list of strings like below to a file separated by the given delimiter.

我正在尝试将如下所示的字符串列表写入由给定分隔符分隔的文件。

res = [u'123', u'hello world']

When I try splitting by TAB like below it gives me the correctly formatted string.

当我尝试按如下 TAB 进行拆分时,它会为我提供格式正确的字符串。

writer = csv.writer(sys.stdout, delimiter="\t")
writer.writerow(res)

gives --> 123   hello world

But when I try to split by space using delimiter=" ", it gives me the space but with quotation marks like below.

但是,当我尝试使用按空格拆分时delimiter=" ",它给了我空格,但​​带有如下引号。

123 "hello world"

How do I remove quotation marks. So that when I use space as the delimiter I should get 123 hello world.

怎么去掉引号。所以当我使用空格作为分隔符时,我应该得到 123 hello world.

EIDT: when I try using the escapechar it doesn't make any double quotes. But everywhere in my testdata it appears a space, it makes it double.

EIDT:当我尝试使用转义字符时,它不会产生任何双引号。但是在我的测试数据中的任何地方都出现一个空格,它使它加倍。

采纳答案by Peter DeGlopper

Quoting behavior is controlled by the various quotingarguments provided to the writer (or set on the Dialectobject if you prefer to do things that way). The default setting is QUOTE_MINIMAL, which will not produce the behavior you're describing unless a value contains your delimiter character, quote character, or line terminator character. Doublecheck your test data - [u'123', u'hello']won't produce what you describe, but [u'123', u' hello']would.

引用行为由quoting提供给作者的各种参数控制(或者,Dialect如果您喜欢这样做,则可以在对象上设置)。默认设置为QUOTE_MINIMAL,除非值包含分隔符、引号或行终止符,否则不会产生您所描述的行为。仔细检查您的测试数据 -[u'123', u'hello']不会产生您所描述的内容,但[u'123', u' hello']会产生。

You can specify QUOTE_NONEif you're sure that's the behavior you want, in which case it'll either try to escape instances of your delimiter character if you set an escape character, or raise an exception if you don't.

您可以指定QUOTE_NONE您是否确定这是您想要的行为,在这种情况下,如果您设置了转义符,它将尝试转义分隔符的实例,否则将引发异常。

回答by Dair

You can set the csv.writerto quote nothing with quoting=csv.QUOTE_NONEfor example:

您可以将 设置为csv.writer不引用任何内容,quoting=csv.QUOTE_NONE例如:

import csv
with open('eggs.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            escapechar=' ', quoting=csv.QUOTE_NONE)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

Produces:

产生:

Spam Spam Spam Spam Spam Baked  Beans
Spam Lovely  Spam Wonderful  Spam

If you do QUOTING_NONEyou also needand escape character.

如果你这样做,QUOTING_NONE你还需要和转义字符。

回答by John Mee

Do you need the csv lib? Just join the strings...

你需要 csv 库吗?只需加入字符串...

>>> res = [u'123', u'hello'] 
>>> print res
[u'123', u'hello']
>>> print " ".join(res)
123 hello

回答by Noaha

What worked for me was using a regular writer, not the csv.writer, and simply use your delimiter in between columns ('\t' in my case):

对我有用的是使用常规编写器,而不是 csv.writer,只需在列之间使用分隔符(在我的情况下为 '\t'):

with open(target_path, 'w', encoding='utf-8') as fd:
     # some code iterating over a pandas daftaframe called mydf     
     # create a string out of column 0, '\t' (tab delimiter) and column 1:
     output = mydf.loc[i][0] + '\t' + mydf.loc[i][1] +'\n'
     # write that output string (line) to the file in every iteration
     fd.write(output)

It might not be the "correct" way but it definitely kept the original lines in my project, which included many strings and quotations.

这可能不是“正确”的方式,但它确实保留了我项目中的原始行,其中包括许多字符串和引号。