|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
Adding Value to CSVI 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. _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Adding Value to CSV"Paras K." <paras80@...> 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) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Adding Value to CSVParas 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. > > 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 _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Adding Value to CSVWhat 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@...> wrote:
_______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Adding Value to CSVParas 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 <davea@... > <mailto:davea@...>> 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 > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@... > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > string you want to the end of the line including end-of-line character. -- Kind Regards, Christian Witts _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Adding Value to CSV(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 <davea@...> 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 - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
| Free embeddable forum powered by Nabble | Forum Help |