使用Python的内置.csv模块编写 [英] Writing with Python's built-in .csv module

查看:298
本文介绍了使用Python的内置.csv模块编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[请注意,这是与已回答的问题不同的问题如何使用Python的内置.csv writer模块替换列?]



我需要做一个查找和在巨大的Excel .csv文件中替换(特定于一列URL)。因为我在开始教自己一个脚本语言的开始阶段,我想我会尝试在python中实现解决方案。



我有麻烦当我尝试在更改条目的内容后写回.csv文件。我阅读了关于如何使用writer的官方csv模块文档,但没有一个涵盖这种情况的例子。具体来说,我试图在一个循环中完成读,替换和写操作。但是,不能在for循环的参数和writer.writerow()的参数中使用相同的row引用。因此,一旦我在for循环中进行了更改,我应该如何写回文件?



编辑:来自S. Lott和Jimmy的建议仍然是相同的结果



编辑#2:我添加了rb和wb按照S. Lott的建议

  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;

在阅读器中的行:
row [-1] = row [-1] .replace('/?','?')
writer.writerow #this是导致问题的行
changed = changed + 1

print('更改的URL总数',已更改)

edit:供您参考,这是解释器的完全追踪:

 跟踪(最近一次调用):
文件C:\Documents和Settings\g41092\ My Documents \palScript .py,第13行,在< module>
在阅读器中的行:
_csv.Error:iterator应该返回字符串,而不是字节(你是否以文本模式打开文件?)


解决方案

您不能读写同一文件。

  source = open(PALTemplateData.csv,rb)
reader = csv.reader(source,dialect)

target = open csv,wb)
writer = csv.writer(target,dialect)

所有文件操作的正常方法是创建原始文件的修改COPY。不要尝试更新文件到位。这是一个不好的计划。






编辑 $ b

在行

  source = open(PALTemplateData.csv,rb)

target = open(AnotherFile.csv,wb)

和wb是绝对必需的。每次忽略这些文件时,您打开文件以错误的格式阅读。



您必须使用rb读取.CSV文件。 Python 2.x没有选择。使用Python 3.x,你可以省略这个,但是明确使用r来表示清楚。



你必须使用wb写一个.CSV文件。 Python 2.x没有选择。使用Python 3.x,你必须使用w。






编辑



出现你正在使用Python3。您需要从rb和wb中删除b。



阅读: http://docs.python.org/3.0/library/functions.html#open


[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 <module>
    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".

Read this: http://docs.python.org/3.0/library/functions.html#open

这篇关于使用Python的内置.csv模块编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆