python中csv模块是自带的吗_使用Python的内置.csv模块编写

在尝试用Python处理大型CSV文件时,遇到读取、替换(URL列)并写回文件的问题。根据官方csv模块文档,尝试在一个循环中完成读、改、写操作,但发现不能在同一循环中使用相同的'row'引用。最终解决方案是创建源文件的修改副本而不是直接在原文件上写入。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

[Please note that this is a different question from the already answered How to replace a column using Python’s built-in .csv writer module?]

I need to do a find and replace (specific to one column of URLs) in a huge Excel .csv file. Since I'm in the beginning stages of trying to teach myself a scripting language, I figured I'd try to implement the solution in python.

I'm having trouble when I try to write back to a .csv file after making a change to the contents of an entry. I've read the official csv module documentation about how to use the writer, but there isn't an example that covers this case. Specifically, I am trying to get the read, replace, and write operations accomplished in one loop. However, one cannot use the same 'row' reference in both the for loop's argument and as the parameter for writer.writerow(). So, once I've made the change in the for loop, how should I write back to the file?

edit: I implemented the suggestions from S. Lott and Jimmy, still the same result

edit #2: I added the "rb" and "wb" to the open() functions, per S. Lott's suggestion

import csv

#filename = 'C:/Documents and Settings/username/My Documents/PALTemplateData.xls'

csvfile = open("PALTemplateData.csv","rb")

csvout = open("PALTemplateDataOUT.csv","wb")

reader = csv.reader(csvfile)

writer = csv.writer(csvout)

changed = 0;

for row in reader:

row[-1] = row[-1].replace('/?', '?')

writer.writerow(row) #this is the line that's causing issues

changed=changed+1

print('Total URLs changed:', changed)

edit: For your reference, this is the new full traceback from the interpreter:

Traceback (most recent call last):

File "C:\Documents and Settings\g41092\My Documents\palScript.py", line 13, in

for row in reader:

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

解决方案

You cannot read and write the same file.

source = open("PALTemplateData.csv","rb")

reader = csv.reader(source , dialect)

target = open("AnotherFile.csv","wb")

writer = csv.writer(target , dialect)

The normal approach to ALL file manipulation is to create a modified COPY of the original file. Don't try to update files in place. It's just a bad plan.

Edit

In the lines

source = open("PALTemplateData.csv","rb")

target = open("AnotherFile.csv","wb")

The "rb" and "wb" are absolutely required. Every time you ignore those, you open the file for reading in the wrong format.

You must use "rb" to read a .CSV file. There is no choice with Python 2.x. With Python 3.x, you can omit this, but use "r" explicitly to make it clear.

You must use "wb" to write a .CSV file. There is no choice with Python 2.x. With Python 3.x, you must use "w".

Edit

It appears you are using Python3. You'll need to drop the "b" from "rb" and "wb".

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值