So I have a CSV file with a bunch on IP's in it:
192.168.0.1,192.168.0.2,192.168.0.3,192.168.0.4,192.168.0.5,192.168.0.6,192.168.0.7,192.168.0.8,192.168.0.9,192.168.0.10
And I would like to add a new ip to the end of this csv file. Currently I am using this code to read in the data:
requests = csv.reader(open("file.csv", "rb"))
for request in requests:
for ip in request:
print "In List: " + str(ip)
This will print:
In List: 192.168.0.1
In List: 192.168.0.2
In List: 192.168.0.3
In List: 192.168.0.4
In List: 192.168.0.5
...
And then to write one to the end I've tried many methods, including this:
requestWriter = csv.writer(open("file.csv", "w"))
requestWriter.writerow(["192.168.0.X"])
This however replaces the whole file with the new entry. I then tried to loop through existing records and add them to the new file but this split the IP's up by their .'s! Am I missing something here? Surely there is an amend option for the csv reader/writer?
Thanks
asked Nov 5, 2010 at 14:59
ingh.am
26.9k44 gold badges133 silver badges180 bronze badges
-
1You are not developing in Windows, isn't it? Because "rb" means read+binary and csv is tipically not binary. This probably works because you are developing on a system that does not differentiate between binary and text files (ie b has no effect).systempuntoout– systempuntoout2010年11月05日 15:14:46 +00:00Commented Nov 5, 2010 at 15:14
-
"and csv is typically not binary". Since for Windows it is binary, it's simpler to open CSV with "wb" to assure compatibility.S.Lott– S.Lott2010年11月05日 15:51:10 +00:00Commented Nov 5, 2010 at 15:51
-
@S.Lott uh, csv is binary in Windows?systempuntoout– systempuntoout2010年11月05日 17:57:54 +00:00Commented Nov 5, 2010 at 17:57
-
@systempuntoout: In an obscure way, yes. From docs.python.org/library/csv.html#csv.writer. "If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference." And the platform where it makes a difference is? You guessed it. Windows.S.Lott– S.Lott2010年11月05日 18:44:04 +00:00Commented Nov 5, 2010 at 18:44
-
-1: "I can't really disclose what system I'm working on". Can't possibly be true unless it was a seekrit custom-built operating system where disclosure of status as the only client would somehow violate national security. Otherwise, it's difficult to be sure of an answer when there's this kind of refusal to disclose basic facts.S.Lott– S.Lott2010年11月05日 19:23:09 +00:00Commented Nov 5, 2010 at 19:23
1 Answer 1
Why don't you just append a new row?
fd = open('file.csv','a')
fd.write(yourCsvRowWithNewIP)
fd.close()
answered Nov 5, 2010 at 15:02
systempuntoout
74.6k47 gold badges176 silver badges247 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
kevingessner
@ing0: You can still use the CSV writer; the important part is opening the file with mode
'a'. See docs.python.org/library/functions.html#open Aaron Digulla
@systempuntoout: Don't forget the comma!
ingh.am
@aaron It's ok, I didn't forget! :)
lang-py