[Tutor] How parse files in function of number of lines
Alan Gauld
alan.gauld at btinternet.com
Thu May 29 02:11:20 CEST 2014
On 28/05/14 20:16, jarod_v6 at libero.it wrote:
> Dear all!
> I have two example files:
> tmp.csv:
> name value root
> mark 34 yes
>
> tmp2.csv
> name value root
>
I understood down to here.
> I want to print a different text if I have more than one row and if I have
> only one row.
This is not clear.
Where do you want to print this text?
What kind of text? More than one row where?
In file 1? file 2? or both?
My code is this:
> with open("tmp.csv") as p:
> header =p.next()
Probably easier to use readline()
> for i in p:
> print i
> g = ()
I've no idea what you think this is doing?
It creates an empty tuple so will always evaluate to False
> if not g:
> print header
> mark 34 yes
>
> no
huh ????
> I want to obtain only where I have only the header the header string? How can
> I do this?Thnks for your great patience and help!
You might want to investigate the csv module and in particular the
DictReader class. It might be easier for your purposes.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list