Hi everyone:
I wrote the following code to transform the data in a csv file and write to another csv file:
<SPAN class="kwd" style="color: #00008b; background: transparent;">import</SPAN><SPAN class="pln" style="background: transparent;"> csv<BR /></SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">import</SPAN><SPAN class="pln" style="background: transparent;"> re<BR />fp </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> r</SPAN><SPAN class="str" style="color: #800000; background: transparent;">'C:\data\input.csv'</SPAN><SPAN class="pln" style="background: transparent;"><BR />fpw </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> r</SPAN><SPAN class="str" style="color: #800000; background: transparent;">'C:\data\output.csv'</SPAN><SPAN class="pln" style="background: transparent;"><BR /><BR /></SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">with</SPAN><SPAN class="pln" style="background: transparent;"> open</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">fp</SPAN><SPAN class="pun" style="background: transparent;">,</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="str" style="color: #800000; background: transparent;">'rb'</SPAN><SPAN class="pun" style="background: transparent;">)</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">as</SPAN><SPAN class="pln" style="background: transparent;"> input</SPAN><SPAN class="pun" style="background: transparent;">,</SPAN><SPAN class="pln" style="background: transparent;"> open</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">fpw</SPAN><SPAN class="pun" style="background: transparent;">,</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="str" style="color: #800000; background: transparent;">'wb'</SPAN><SPAN class="pun" style="background: transparent;">)</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">as</SPAN><SPAN class="pln" style="background: transparent;"> output</SPAN><SPAN class="pun" style="background: transparent;">:</SPAN><SPAN class="pln" style="background: transparent;"><BR /></SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;"> for</SPAN><SPAN class="pln" style="background: transparent;"> row </SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">in</SPAN><SPAN class="pln" style="background: transparent;"> csv</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="pln" style="background: transparent;">reader</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">input</SPAN><SPAN class="pun" style="background: transparent;">):</SPAN>
<SPAN class="pun" style="background: transparent;"></SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;"> try</SPAN><SPAN class="pun" style="background: transparent;">:</SPAN>
<SPAN class="pun" style="background: transparent;"></SPAN><SPAN class="pln" style="background: transparent;"> stop </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> row</SPAN><SPAN class="pun" style="background: transparent;">[</SPAN><SPAN class="lit" style="color: #800000; background: transparent;">11</SPAN><SPAN class="pun" style="background: transparent;">] # find a particular field</SPAN><SPAN class="pln" style="background: transparent;"><BR /> extr </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> re</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="pln" style="background: transparent;">search</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">r</SPAN><SPAN class="str" style="color: #800000; background: transparent;">"\[([A-Za-z0-9_]+)\]"</SPAN><SPAN class="pun" style="background: transparent;">,</SPAN><SPAN class="pln" style="background: transparent;"> stop</SPAN><SPAN class="pun" style="background: transparent;">) # extract value enclosed by brackets in that field</SPAN><SPAN class="pln" style="background: transparent;"><BR /> stop_id </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> str</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">extr</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="pln" style="background: transparent;">group</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="lit" style="color: #800000; background: transparent;">1</SPAN><SPAN class="pun" style="background: transparent;">)) </SPAN><SPAN class="pln" style="background: transparent;"><BR /> row</SPAN><SPAN class="pun" style="background: transparent;">[</SPAN><SPAN class="lit" style="color: #800000; background: transparent;">11</SPAN><SPAN class="pun" style="background: transparent;">]</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> stop_id # replace the original field with the extracted value<BR /> repl_row </SPAN><SPAN class="pun" style="background: transparent;">=</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="str" style="color: #800000; background: transparent;">','</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="pln" style="background: transparent;">join</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">row</SPAN><SPAN class="pun" style="background: transparent;">)</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="pun" style="background: transparent;">+</SPAN><SPAN class="pln" style="background: transparent;"> </SPAN><SPAN class="str" style="color: #800000; background: transparent;">'\n'</SPAN><SPAN class="pln" style="background: transparent;"><BR /> output</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="pln" style="background: transparent;">write</SPAN><SPAN class="pun" style="background: transparent;">(</SPAN><SPAN class="pln" style="background: transparent;">repl_row</SPAN><SPAN class="pun" style="background: transparent;">) # write the tweaked row to another csv file</SPAN><SPAN class="pln" style="background: transparent;"><BR /> </SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">except</SPAN><SPAN class="pln" style="background: transparent;"> csv</SPAN><SPAN class="pun" style="background: transparent;">.</SPAN><SPAN class="typ" style="color: #2b91af; background: transparent;">Error</SPAN><SPAN class="pun" style="background: transparent;">:</SPAN><SPAN class="pln" style="background: transparent;"><BR /> </SPAN><SPAN class="kwd" style="color: #00008b; background: transparent;">pass</SPAN>
The code worked half way through the file and stopped due to an error raised, that said 'for row in csv.reader, line contains NULL byte'. I want to skip this error and proceed. Despite the except statement, it still stopped. Does anyone know how to solve this issue?
Much appreciate, Sui