[Tutor] Adding Value to CSV

Dave Angel davea at ieee.org
Sun Nov 1 13:16:24 CET 2009


Paras K. wrote:
> I have some code that is going through a number of test.
>
> 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
>
> Example of my code:
>
> for line in fh.readlines():
>             readline = line
>             ipline = readline
>             ip = ipline.split(' ')[0]
>             split_ip = ip.split('.')
>             if ((split_ip[0] == '"152')):
>                 ff.write(readline)
>                 totalcount +=1
>
>
> I need to add another piece, how can I add another field at the end of
> ff.write(readline)
>
>
> Any assistance would be greatly appreciated. Thank You.
>
>   
If your program is correct so far, then you could add it simply with:
             ff.write(readline + " " + newdata)

Although your message subject says CSV, it looks like you're breaking 
the line up by spaces.  So if in fact each field is constrained not to 
have a space within, and there is a single space between fields, then 
the above will work.

If, on the other hand, you have to deal with fields that can contain the 
delimiter, perhaps escaped or quoted, then things get much more complicated.

DaveA


More information about the Tutor mailing list