<div class="gmail_quote">Hello!<div>My name is Denis Kolodin. I live in Russia, Tambov.<div>I was developing much time with C, Java, C#, R. But two month ago I'm using Python.</div><div>It's really cool. Now, I move ALL my projects to it fully and have some ideas which API's</div>
<div>extensions may will be useful.</div><div>The first thing I want to say about is an extension of CSV api. In R language I could to set types for</div><div>the every column in a csv file. I propose to add a same function to the Python's standard library.</div>
<div>Here it is (Python 3 version):</div><div><span style="font-family:'Times New Roman';font-size:medium"><pre style="background-color:transparent;border-top-width:1px;border-top-style:none;border-top-color:rgb(204, 204, 204);border-bottom-width:1px;border-bottom-style:none;border-bottom-color:rgb(204, 204, 204);padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px;font-size:13px;font-family:'Bitstream Vera Sans Mono', monospace;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;border-right-style:none;border-left-style:none;border-width:initial;border-color:initial;overflow-x:auto;overflow-y:auto">
<span style="color:rgb(0, 112, 32);font-weight:bold">import</span> <span style="color:rgb(14, 132, 181);font-weight:bold">csv</span>
<span style="color:rgb(0, 112, 32);font-weight:bold">def</span> <span style="color:rgb(6, 40, 126)">reader2</span><span>(</span><span>csvfile</span><span>,</span> <span>frame</span><span>,</span> <span><b>delimiter</b></span><span style="color:rgb(102, 102, 102)"><b>=</b></span><span style="color:rgb(64, 112, 160)"><b>';'</b></span><span>,</span> <span style="color:rgb(102, 102, 102)">**</span><span>fmtparams</span><span>):</span>
<span>reader</span> <span style="color:rgb(102, 102, 102)">=</span> <span>csv</span><span style="color:rgb(102, 102, 102)">.</span><span>reader</span><span>(</span><span>csvfile</span><span>,</span> delimiter=delimiter, <span style="color:rgb(102, 102, 102)">**</span><span>fmtparams</span><span>)</span>
<span style="color:rgb(0, 112, 32);font-weight:bold">for</span> <span>row</span> <span style="color:rgb(0, 112, 32);font-weight:bold">in</span> <span>reader</span><span>:</span>
<span>l</span> <span style="color:rgb(102, 102, 102)">=</span> <span style="color:rgb(0, 112, 32)">min</span><span>(</span><span style="color:rgb(0, 112, 32)">len</span><span>(</span><span>row</span><span>),</span> <span style="color:rgb(0, 112, 32)">len</span><span>(</span><span>frame</span><span>))</span>
<span style="color:rgb(0, 112, 32);font-weight:bold">yield</span> <span>[</span><span>frame</span><span>[</span><span>idx</span><span>](</span><span>row</span><span>[</span><span>idx</span><span>])</span> <span style="color:rgb(0, 112, 32);font-weight:bold">for</span> <span>idx</span> <span style="color:rgb(0, 112, 32);font-weight:bold">in</span> <span style="color:rgb(0, 112, 32)">range</span><span>(</span><span>l</span><span>)]</span></pre>
</span></div><div>This's generator function which converts an every column to the associated type.</div><div>In <b><i>frame </i></b>argument you must to set tuple/list of functions which will uses to</div><div>convert values in same positions of row from csv file. Frame looks like list of types )))</div>
<div>By default it uses ';' delimiter to make float values conversion are possible.</div><div><br></div><div>As a sample you have the csv file like:</div><div><b><i>Any spam...; 1; 2.0; 3</i></b></div><div><br></div>
<div>I've saved it to "sample.csv" :)</div><div><br></div><div>If you are using function reader in the standard "csv" module you get rows as a list of strings :(</div><div><i>>>> reader = csv.reader(open("sample.csv"), delimiter=";")</i></div>
<div><div><i>>>> print(next(reader))</i></div><div><i>['Any spam...', ' 1', ' 2.0', ' 3']</i></div><div><i><br></i></div><div><i><span style="font-style:normal"><div>
It's not bad in certan situatiuons. But with "reader2" function you can get a list with necessary types:</div><div><br></div><div><div><i>>>> reader = reader2(open("foodstuffs.csv"), (str, int, float, int))</i></div>
<div><i>>>> print(next(reader))</i></div><div><i>['Any spam...', 1, 2.0, 3]</i></div><div><br></div><div>Now you can work with items without extra conversions. <img src="cid:360@goomoji.gmail" style="margin-top:0px;margin-right:0.2ex;margin-bottom:0px;margin-left:0.2ex;vertical-align:middle" goomoji="360"></div>
<div>I think it's good to add this function to the standard library. I've already used it many times.</div><div>This function can be useful for many people who works with csv files.</div><div>And I suppose it conforms to "batteries included" philosophy.</div>
<div><br></div><div>What do you think about this extension?</div><div>Is it possible to add this function to standard library or to add the same behavior to</div><div>the standard "readed" function <i><span style="font-style:normal"><div style="display:inline !important">
<div style="display:inline !important">in "csv" Python's module?</div></div></span></i></div><div><br></div><div>Best Regards,</div><div>Denis Kolodin</div></div><div>Russia, Tambov</div></span></i></div></div>
</div>
</div><br>