[Tutor] Adding Value to CSV
Paras K.
paras80 at gmail.com
Mon Nov 2 10:14:36 CET 2009
What I am trying to do is as I am writing the row to the CSV file, I want to
add the string base on a few other checks that I still need to write.
Ex.
readline = '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23
15:40:33"\r\n'
At the end of this based on my checks I want to be able to write a string
like
Part of DHCP Pool or Part of VPN Pool
So the final line should be something like this written to the CSV file:
'"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 15:40:33",
"Part of DHCP Pool"
Thanx in advance for the help.
On Sun, Nov 1, 2009 at 7:16 AM, Dave Angel <davea at ieee.org> wrote:
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091102/d2e3fd36/attachment.htm>
More information about the Tutor
mailing list