[Tutor] Adding Value to CSV

Alan Gauld alan.gauld at btinternet.com
Sun Nov 1 01:05:23 CET 2009

"Paras K." <paras80 at gmail.com> wrote

> When I have the line that has been read I need to add another value at 
> the
> end of it, or write the information into another csv file

If you are dealing with csv fioles you should look at using the csv module.

> for line in fh.readlines():
>            readline = line
>            ipline = readline

the readline and ipline variables are not needed. just use line!

>            ip = ipline.split(' ')[0]
>            split_ip = ip.split('.')
>            if ((split_ip[0] == '"152')):

you don't need the parentheses here, its not C.
And you definitely don't need two sets of them!

>                ff.write(readline)
>                totalcount +=1
> I need to add another piece, how can I add another field at the end of
> ff.write(line)

just add it to the end of line before writing it using any of the
normal string addition operations, eg:

line += newfield    or
line = "%s%s" % (line,newfield)


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list