Python 2.7.1:如何打开,编辑和关闭CSV文件 [英] Python 2.7.1: How to Open, Edit and Close a CSV file

查看:1839
本文介绍了Python 2.7.1:如何打开,编辑和关闭CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法打开一个文件(amount2.csv)进行更改,保存并关闭该文件。

I'm having trouble opening a file (amount2.csv) making a change, saving it and closing the file.

如何打开文件编辑,保存并关闭它?

How does one open a file edit, save and close it?

import csv

changes = {
    '1 dozen' : '12'
    }
with open('amount2.csv', 'r') as f:
reader = csv.reader(f)
print f
f.close()

我的错误:打开文件'amount2.csv'在0x1004656f0
(<>已删除)

my error: open file 'amount2.csv', mode 'r' at 0x1004656f0 (<> removed)

推荐答案

<open file 'amount2.csv', mode 'r' at 0x1004656f0>

您看到的不是错误,而是'打印f'的结果。要改为查看文件的内容,您可以使用open('test.csv','rb')做

you are seeing isn't an error, but the result of your 'print f'. To instead see the contents of your file, you would do

with open('test.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        # row is a list of strings
        # use string.join to put them together
        print ', '.join(row)

要将行追加到文件中, p>

To append rows to your file, instead do

changes = [    
    ['1 dozen','12'],                                                            
    ['1 banana','13'],                                                           
    ['1 dollar','elephant','heffalump'],                                         
    ]                                                                            

with open('test.csv', 'ab') as f:                                    
    writer = csv.writer(f)                                                       
    writer.writerows(changes)


$ b b

有关 Python CSV文档的更多信息

编辑:

我误解了,你想在csv文件中将'1打'的所有条目改为'12'。我将首先说,这是更容易做没有使用csv模块,但这里是一个使用它的解决方案。

I misunderstood at first, you want to change all entries of '1 dozen' to '12' in your csv file. I will say first, this is easier to do without using the csv module, but here is a solution using it.

import csv

new_rows = [] # a holder for our modified rows when we make them
changes = {   # a dictionary of changes to make, find 'key' substitue with 'value'
    '1 dozen' : '12', # I assume both 'key' and 'value' are strings
    }

with open('test.csv', 'rb') as f:
    reader = csv.reader(f) # pass the file to our csv reader
    for row in reader:     # iterate over the rows in the file
        new_row = row      # at first, just copy the row
        for key, value in changes.items(): # iterate over 'changes' dictionary
            new_row = [ x.replace(key, value) for x in new_row ] # make the substitutions
        new_rows.append(new_row) # add the modified rows

with open('test.csv', 'wb') as f:
    # Overwrite the old file with the modified rows
    writer = csv.writer(f)
    writer.writerows(new_rows)

如果你刚接触编程和python,最麻烦的行可能是

If you're new to programming and python the most trobulesome line is probably

new_row = [ x.replace(key, value) for x in new_row ]

但这只是一个列表推导, / p>

but this is just a list comprehension that is effectively equivalent to

temp = []
for x in new_row:
    temp.append( x.replace(key, value) )
new_row = temp

这篇关于Python 2.7.1:如何打开,编辑和关闭CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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