<br><font size=3 face="Times New Roman">Hi Dave,</font>
<br>
<br><font size=2><tt>> From: dave s <pythontut@pusspaws.net><br>
> Subject: [Tutor] A list in list problem<br>
> To: python tutor <tutor@python.org><br>
> Message-ID: <200608210959.01834.pythontut@pusspaws.net><br>
> Content-Type: text/plain; charset="us-ascii"<br>
</tt></font>
<br><font size=2><tt>> def CSV_Lines(self, csv, from_, to):<br>
> """Returns a list of cleaned up lines from csv 'from_'
line</tt></font>
<br><font size=2><tt>> number 'to' line number"""<br>
</tt></font>
<br><font size=2><tt>> clean_line
= clean_csv = []<br>
> for string in range(from_, to):</tt></font>
<br><font size=2><tt>>
split_string = csv[string].split(',')<br>
> split_string
= split_string[1:-1] # Strip the LHS column + the /n'</tt></font>
<br><font size=2><tt>>
if split_string[0] == '' : continue # Skip
empty lines<br>
> print
'##########################'<br>
> print
'split_string ', split_string<br>
> for
string in split_string:</tt></font>
<br><font size=2><tt>>
if len(string)
> 0: clean_line.append(string[1:-1])<br>
> print
'clean_line ',clean_line<br>
> clean_csv.append(clean_line)<br>
> print
'clean_csv ',clean_csv<br>
> clean_line
= []</tt></font>
<br><font size=2><tt>> <br>
> But I get clean_csv trouble ...<br>
</tt></font>
<br><font size=2><tt>> ubuntu@ubuntu:~/python_develop/unison/PxQxAudit/main$
./crosscheck.py<br>
> ##########################<br>
> split_string ['"temp1"', '"wow a variable"',
'', '', '']<br>
> clean_line ['temp1', 'wow a variable']<br>
> clean_csv ['temp1', 'wow a variable', [...]]<br>
> ##########################<br>
> split_string ['"temp2"', '', '', '', '']<br>
> clean_line ['temp2']<br>
> clean_csv ['temp1', 'wow a variable', [...], ['temp2']]<br>
> ubuntu@ubuntu:~/python_develop/unison/PxQxAudit/main$<br>
</tt></font>
<br><font size=2><tt>> ie clean_csv ends up as ['temp1', 'wow a variable',
[...], ['temp2']] instead<br>
> 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> csv = csv[from_ : to]
# We are interested just here</tt></font>
<br><font size=3><tt> csv = [line.split(',') for line in csv]
# Make a 2D array from the list</tt></font>
<br><font size=3><tt> return [LineItems[1:-1] for LineItems
in csv if len(LineItems) > 2] </tt></font>
<br><font size=3><tt>
# filter out first and last columns,
and lines with too less items</tt></font>
<br>
<br>
<br>
<br><font size=3><tt>csv = """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"""</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>>>>[['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>