[Tutor] Problem on filtering data
Steven D'Aprano
steve at pearwood.info
Mon Jun 8 18:02:38 CEST 2015
On Mon, Jun 08, 2015 at 04:50:13PM +0200, jarod_v6--- via Tutor wrote:
> Dear All;
> I have a very silly problem.
The right way to handle CSV files is to use the csv module:
https://docs.python.org/2/library/csv.html
http://pymotw.com/2/csv/
Some more comments further below.
> with open("Dati_differenzialigistvsminigist_solodiff.csv") as p:
> for i in p:
> lines = i.strip("\n").split("\t")
> if lines[8] != "NA":
> if lines[8] :
> print lines[8]
>
> Why I continue to obtain "" empity line?
I don't know. What do you mean 'obtain "" empty line'? Do you mean that
every line is empty? Some of the lines are empty? One empty line at the
end? How do you know it is empty? You need to explain more.
Perhaps put this instead:
print repr(lines[8])
to see what it contains. Perhaps there are invisible s p a c e s so it
only looks empty.
Some more comments:
In English, we would describe open("Dati....csv") as a file, and write
with open("Dati....csv") as f: # f for file
I suppose in Italian you might say "archivio", and write:
with open("Dati....csv") as a:
But what is "p"?
The next line is also confusing:
for i in p:
"for i in ..." is used for loops over the integers 0, 1, 2, 3, ... and
should not be used for anything else. Here, you loop over the lines in
the file, so you should write:
for line in p:
fields = line.strip("\n").split("\t")
or perhaps "columns" instead of "fields". But "lines" is completely
wrong, because you are looking at one line at a time.
--
Steve
More information about the Tutor
mailing list