csv编写器在python中添加两次双引号

时间:2022-09-15 15:23:12

I have written a python script to append href attribute and tag to a string holding a url which is in a csv file. The problem is that the result is not as expected. The resulted html string has got an extra double quote instead of single. Any suggestion how to fix this? Below is the snippet:

我编写了一个python脚本来将href属性和标记附加到一个字符串中,该字符串包含一个位于csv文件中的url。问题是结果不如预期。生成的html字符串有一个额外的双引号而不是单引号。有任何建议如何解决这个问题?以下是片段:

InputFile = open('data1.csv', 'rb')
OutputFile = open('data3.csv', 'a+b')

CsvReader_InputFile = csv.reader(InputFile, delimiter=',', quotechar='\"')
CsvWriter_OutputFile = csv.writer(OutputFile, delimiter=',', quotechar='\"')

Row_InputFile = CsvReader_InputFile.next()
Row_InputFile[2] = "<a href=\"" + Row_InputFile[2] + "\">Link</a>"
CsvWriter_OutputFile.writerow(Row_InputFile)

Output:

"<a href=""http://www.google.com"">Link</a>"

Wanted Output:

"<a href="http://www.google.com">Link</a>"

1 个解决方案

#1


2  

This is correct behaviour. Double quotes are escaped inside csv value.

这是正确的行为。双引号在csv值内转义。

If you want to output without escaping try csv.QUOTE_NONE

如果要在不转义的情况下输出,请尝试csv.QUOTE_NONE

csv.writer(OutputFile, delimiter=',', quotechar='\"', quoting=csv.QUOTE_NONE)

#1


2  

This is correct behaviour. Double quotes are escaped inside csv value.

这是正确的行为。双引号在csv值内转义。

If you want to output without escaping try csv.QUOTE_NONE

如果要在不转义的情况下输出,请尝试csv.QUOTE_NONE

csv.writer(OutputFile, delimiter=',', quotechar='\"', quoting=csv.QUOTE_NONE)