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:
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>"
Since CSV files use the comma character "," to separate columns, values that contain commas must be handled as a special case. These fields are wrapped within double quotation marks. The first double quote signifies the beginning of the column data, and the last double quote marks the end.
There are 2 accepted ways of escaping double-quotes in a CSV file. One is using a 2 consecutive double-quotes to denote 1 literal double-quote in the data. The alternative is using a backslash and a single double-quote.
Using the strip() Function to Remove Double Quotes from String in Python. We use the strip() function in Python to delete characters from the start or end of the string. We can use this method to remove the quotes if they exist at the start or end of the string.
quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote ( ' " ' ). escapechar specifies the character used to escape the delimiter character, in case quotes aren't used.
This is correct behaviour. Double quotes are escaped inside csv value.
If you want to output without escaping try csv.QUOTE_NONE
csv.writer(OutputFile, delimiter=',', quotechar='\"', quoting=csv.QUOTE_NONE)
I ran into this when trying to store a JSON object as a string in a column in a csv. I had single quotes around a string filled with double quotes. my row of data looked like
['foo', 'bar', '{"foo":"bar". "bar":"foo"}']
Doing this solved it for me
writer = csv.writer(csv_file, delimiter=',', quotechar="\'", quoting=csv.QUOTE_NONE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With