Python string.replace is stripping off my quote characters
<p>What I'm doing is feeding my python script a CSV file which contains millions of records separated by commas. Any strings are "contained by double qoutes".</p>

<p>I pass this .csv file through my python script</p>

<pre><code>import csv
import string
import sys, getopt

inFile = open(sys.argv[1], 'r')
outFile = open(sys.argv[1][:-4] + '_no-nulls.csv', 'w')
data = csv.reader(inFile)
writer = csv.writer(outFile)

specials = "NULL"

for line in data:
    line = [value.replace(specials, '') for value in line]
    writer.writerow(line)

inFile.close()
outFile.close()
</code></pre>

<p>And the end result has all the quotes stipped off my strings.<br>
What am I doing wrong?</p>

<h2>Edit</h2>

<p>Sample input:</p>

<pre><code>897555,2021-03-31 00:00:00.000,NULL,"45687","B","QA",29,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"5648987QEXXX",6,NULL,NULL,"DOE","JOHN",NULL,NULL,NULL,NULL,NULL,"Q",1994-04-24 00:00:00.000,"R","CX","ZZ",NULL,NULL,NULL,NULL,NULL,"Y",NULL,"GA","R","DE",NULL,NULL,NULL,NULL,NULL,"EN",NULL,"Y","OP",NULL,"R","XZ",NULL,NULL,NULL,"8945564",2005-03-01 12:00:00.000,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
</code></pre>

<p>Sample output:</p>

<pre><code>897555,2021-03-31 00:00:00.000,,"45687","B","QA",29,,,,,,,,"5648987QEXXX",6,,,"DOE","JOHN",,,,,,"Q",1994-04-24 00:00:00.000,"R","CX","ZZ",,,,,,"Y",,"GA","R","DE",,,,,,"EN",,"Y","OP",,"R","XZ",,,,"8945564",2005-03-01 12:00:00.000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
</code></pre>

<p>This is normal. When reading, <code>csv.reader</code> will strip off the quotes because it's assumed that the program consuming the data doesn't want or need them. <code>csv.writer</code> will then put them back on if necessary, depending on the setting of <a href="https://docs.python.org/3/library/csv.html#csv.Dialect.quoting" rel="nofollow"><code>quoting</code></a> that you pass, the default being <code>QUOTE_MINIMAL</code> - it will only add quotes if there are characters in the string that could be misinterpreted.</p>

<p>You could set both the reader and the writer to <code>QUOTE_NONE</code> to preserve the quotes that are in the original file, or set the writer to <code>QUOTE_ALL</code> to requote all output.</p>

python string csv replace