简体   繁体   English

CSV文件中不需要的引号

[英]Unwanted quotation marks in CSV file

I have a code that looks like this: 我有一个看起来像这样的代码:

with open('Recipe 2.txt', 'w') as fp:
    a = csv.writer(fp)
    data = [['VER:3;'],
            ['Recipe 0;'],
            ['Last[1,1];'+d+';'],
    a.writerows(data)

d is defined earlier. d是较早定义的。

My problem is that when i write this to a text file, i get quotationmarks around the last row like this: 我的问题是,当我将其写入文本文件时,我在最后一行周围得到引号,如下所示:

Recipe 0;
"Last[1,1];1.5;"

How can I get rid of the quotationmarks on the last line? 如何摆脱最后一行的引号?

Best, Jonas 最好,乔纳斯

I assume you want your CSV file to be like 我假设您希望CSV文件像

VER:3
Recipe 0
Last[1,1]; <content_of_d>

your data should look something like this 您的数据应如下所示

data = [
    ['VER:3'],
    ['Recipe 0'],
    ['Last[1,1]', d]
]

Where each list is a row and each string in the row is a field. 其中每个列表是一行,并且该行中的每个字符串是一个字段。 I guess those semicolon you put there are the separators, there is no need to insert them, the writer will do that for you. 我猜您放入的分号有分隔符,无需插入分隔符,作者将为您完成。 The separator it uses depends which dialect you configured it with. 它使用的分隔符取决于您为其配置的方言。

Thank you for the help! 感谢您的帮助!

I found that it was the comma INSIDE the bracket that was creating the quotation marks. 我发现创建引号的是括号内的逗号。 I simply edited it from: ['Last[1,1];'+d+';'], to ['Last[1];'+d+';'], 我只是将它从['Last [1,1];'+ d +';']编辑为['Last [1];'+ d +';'],

And I got rid of the quotationmarks. 而且我摆脱了引号。

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM