<br><font size=3 face="Times New Roman">Hi Dave,</font>
<br>
<br><font size=2><tt>&gt; From: dave s &lt;pythontut@pusspaws.net&gt;<br>
&gt; Subject: [Tutor] A list in list problem<br>
&gt; To: python tutor &lt;tutor@python.org&gt;<br>
&gt; Message-ID: &lt;200608210959.01834.pythontut@pusspaws.net&gt;<br>
&gt; Content-Type: text/plain; &nbsp;charset=&quot;us-ascii&quot;<br>
</tt></font>
<br><font size=2><tt>&gt; def CSV_Lines(self, csv, from_, to):<br>
&gt; &quot;&quot;&quot;Returns a list of cleaned up lines from csv 'from_'
line</tt></font>
<br><font size=2><tt>&gt; number &nbsp;'to' line number&quot;&quot;&quot;<br>
</tt></font>
<br><font size=2><tt>&gt; &nbsp; &nbsp; &nbsp; &nbsp; clean_line
= clean_csv = []<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; for string in range(from_, to):</tt></font>
<br><font size=2><tt>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; split_string = csv[string].split(',')<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; split_string
= split_string[1:-1] &nbsp;# Strip the LHS column + the /n'</tt></font>
<br><font size=2><tt>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; if split_string[0] == '' : continue &nbsp;# Skip
empty lines<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
'##########################'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
'split_string ', split_string<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for
string in split_string:</tt></font>
<br><font size=2><tt>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(string)
&gt; 0: clean_line.append(string[1:-1])<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
'clean_line ',clean_line<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clean_csv.append(clean_line)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print
'clean_csv ',clean_csv<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clean_line
= []</tt></font>
<br><font size=2><tt>&gt; <br>
&gt; But I get clean_csv trouble &nbsp;...<br>
</tt></font>
<br><font size=2><tt>&gt; ubuntu@ubuntu:~/python_develop/unison/PxQxAudit/main$
./crosscheck.py<br>
&gt; ##########################<br>
&gt; split_string &nbsp;['&quot;temp1&quot;', '&quot;wow a variable&quot;',
'', '', '']<br>
&gt; clean_line &nbsp;['temp1', 'wow a variable']<br>
&gt; clean_csv &nbsp;['temp1', 'wow a variable', [...]]<br>
&gt; ##########################<br>
&gt; split_string &nbsp;['&quot;temp2&quot;', '', '', '', '']<br>
&gt; clean_line &nbsp;['temp2']<br>
&gt; clean_csv &nbsp;['temp1', 'wow a variable', [...], ['temp2']]<br>
&gt; ubuntu@ubuntu:~/python_develop/unison/PxQxAudit/main$<br>
</tt></font>
<br><font size=2><tt>&gt; ie clean_csv ends up as ['temp1', 'wow a variable',
[...], ['temp2']] instead<br>
&gt; of [[temp1, wow a variable], [temp2]]<br>
</tt></font>
<br>
<br><font size=2><tt>You have used string as variable name two times, once
as an integer and once as a field in the line.</tt></font>
<br><font size=2><tt>It should be avoided.</tt></font>
<br>
<br><font size=2><tt>I like list comprehension in this case.</tt></font>
<br>
<br><font size=3><tt>def CSV_Lines2(csv, from_, to):</tt></font>
<br><font size=3><tt>&nbsp; &nbsp; csv = csv[from_ : to] &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; # We are interested just here</tt></font>
<br><font size=3><tt>&nbsp; &nbsp; csv = [line.split(',') for line in csv]
&nbsp; &nbsp; &nbsp; &nbsp; # Make a 2D array from the list</tt></font>
<br><font size=3><tt>&nbsp; &nbsp; return [LineItems[1:-1] for LineItems
in csv if len(LineItems) &gt; 2] </tt></font>
<br><font size=3><tt>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; # filter out first and last columns,
and lines with too less items</tt></font>
<br>
<br>
<br>
<br><font size=3><tt>csv = &quot;&quot;&quot;Header1</tt></font>
<br><font size=3><tt>Header2</tt></font>
<br><font size=3><tt>temp1,12,20,1</tt></font>
<br><font size=3><tt>temp2,22,22,2</tt></font>
<br><font size=3><tt>temp3,33,44,3</tt></font>
<br><font size=3><tt>temp4,34,64,4</tt></font>
<br><font size=3><tt>Footer1</tt></font>
<br><font size=3><tt>Footer2&quot;&quot;&quot;</tt></font>
<br>
<br><font size=3><tt>csv = csv.split('\n')</tt></font>
<br><font size=3><tt>print CSV_Lines2(csv, 2, 6)</tt></font>
<br><font size=3><tt>&gt;&gt;&gt;[['12', '20'], ['22', '22'], ['33', '44'],
['34', '64']]</tt></font>
<br><font size=3 face="Times New Roman"><br>
<br>
Yours sincerely, <br>
______________________________<br>
Janos Juhasz</font><font size=3> </font><font size=3 color=#cc0000 face="Times New Roman"><br>
</font>