(Please don't top-post on a newsgroup that has the convention of adding
new content after quoted text.)
Paras K. wrote:
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 <[email protected]> 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
Now that your input format is entirely different than what your code can
parse, my answer would have to be different as well. If you were still
doing it by hand, then you'd need to use strip() to remove the trailing
newline, then concatenate with a comma, some quotes, and another newline.
But you probably ought to be using the csv module, since you're likely
to come up with some fields having embedded commas in them, and split()
cannot handle that.
Once you've got a csv.reader() and a csv.writer(), all you're looking
for is mydata.append(newdata) to add newdata field to the end of a
record. The reader reads into a list, and the write writes from a list.
DaveA
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor