<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Find and replace blanks in CSV with python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673632#M52102</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a csv with some blanks that I want to convert to a value of N so I can import into a database.&amp;nbsp; Can someone tell me what I'm doing wrong in my code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import fileinput&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for line in fileinput.FileInput(OPS_frq,inplace=1):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = line.replace(" ","N")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print line&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The csv looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GEOKEY,INDIG,CAVE,CORR,EYPT,PSP,LIC,HERIT,WELLS,TS,TT&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1,,,,,,,,,,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2,,,,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3,,,,Y&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Dec 2010 03:57:50 GMT</pubDate>
    <dc:creator>AngelaPaltridge</dc:creator>
    <dc:date>2010-12-15T03:57:50Z</dc:date>
    <item>
      <title>Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673632#M52102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a csv with some blanks that I want to convert to a value of N so I can import into a database.&amp;nbsp; Can someone tell me what I'm doing wrong in my code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import fileinput&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for line in fileinput.FileInput(OPS_frq,inplace=1):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = line.replace(" ","N")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print line&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The csv looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GEOKEY,INDIG,CAVE,CORR,EYPT,PSP,LIC,HERIT,WELLS,TS,TT&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1,,,,,,,,,,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2,,,,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3,,,,Y&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Dec 2010 03:57:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673632#M52102</guid>
      <dc:creator>AngelaPaltridge</dc:creator>
      <dc:date>2010-12-15T03:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673633#M52103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;well you are telling the line to replace " " with "N". Try "" instead of " ". This is replacing an empty string instead of a string that contains only a space character.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Dec 2010 13:07:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673633#M52103</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2010-12-15T13:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673634#M52104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;well you are telling the line to replace " " with "N". Try "" instead of " ". This is replacing an empty string instead of a string that contains only a space character.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I try that, I get this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;NGNENONKNENYN,NINNNDNINGN,NCNANVNEN,NCNONRNRN,NENXNPNTN,NPNSNPN,NLNINCN,NHNENRNINTN,NWNENLNLNSN,NTNSN,NTNTN&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N1N0N1N0N0N0N1N0N0N0N0N,N,N,N,N,N,N,N,N,N,N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N1N0N1N0N0N1N1N0N0N0N0N,N,N,N,N,N,N,N,N,N,N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N1N0N1N0N0N1N1N0N1N0N0N,N,N,N,N,N,N,N,N,N,N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;N1N0N1N0N0N3N1N2N3N0N0N,N,N,N,N,NYN&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Dec 2010 03:17:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673634#M52104</guid>
      <dc:creator>AngelaPaltridge</dc:creator>
      <dc:date>2010-12-20T03:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673635#M52105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This won't work, you're going to need to use the&amp;nbsp; &lt;/SPAN&gt;&lt;A href="http://docs.python.org/release/2.6.6/library/csv.html#module-csv" rel="nofollow noopener noreferrer" target="_blank"&gt;CSV module&lt;/A&gt;&lt;SPAN&gt; to look deeper into your data. Yo could do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import csv

in_file = "hello.csv"
out_file = "hello_fixed.csv"

row_reader = csv.reader(open(in_file, "rb"))
row_writer = csv.writer(open(out_file, "wb"))

first_row = row_reader.next()
row_writer.writerow(first_row)
for row in row_reader:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_row = [val if val else "N" for val in row] + (["N"] * (len(first_row) - len(row)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row, "-&amp;gt;", new_row
&amp;nbsp;&amp;nbsp;&amp;nbsp; row_writer.writerow(new_row)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Even though this is plain text, it's pretty tricky to get right without the help of a library.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:25:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673635#M52105</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2021-12-12T04:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673636#M52106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;This won't work, you're going to need to use the&amp;nbsp; &lt;A href="http://docs.python.org/release/2.6.6/library/csv.html#module-csv" rel="nofollow noopener noreferrer" target="_blank"&gt;CSV module&lt;/A&gt; to look deeper into your data. Yo could do something like this:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import csv

in_file = "hello.csv"
out_file = "hello_fixed.csv"

row_reader = csv.reader(open(in_file, "rb"))
row_writer = csv.writer(open(out_file, "wb"))

first_row = row_reader.next()
row_writer.writerow(first_row)
for row in row_reader:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_row = [val if val else "N" for val in row] + (["N"] * (len(first_row) - len(row)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row, "-&amp;gt;", new_row
&amp;nbsp;&amp;nbsp;&amp;nbsp; row_writer.writerow(new_row)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Even though this is plain text, it's pretty tricky to get right without the help of a library.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Jason - that worked brilliantly!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:25:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/673636#M52106</guid>
      <dc:creator>AngelaPaltridge</dc:creator>
      <dc:date>2021-12-12T04:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace blanks in CSV with python</title>
      <link>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/1052877#M60950</link>
      <description>&lt;P&gt;I mean Python *is* great but I want to add to the thread that the import wizard in MS Excel is pretty flexible with a lot of delimited (and fixed-column) data -- so there is absolutely nothing wrong with using the excel text file wizard to get to excel, fix your data up, and export again as csv for import to ArcGIS. Just sayin.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Apr 2021 19:15:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/find-and-replace-blanks-in-csv-with-python/m-p/1052877#M60950</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-04-29T19:15:36Z</dc:date>
    </item>
  </channel>
</rss>

