python修改csv文件脚本,在Python中修改csv文件

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

解决方案

Why don't you just append a new row?

fd = open('file.csv','a')

fd.write(yourCsvRowWithNewIP)

fd.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值