简体   繁体   English

写入多个.csv文件

[英]Writing multiple .csv files

I have a dictionary where item_dictionary[0] corresponds to data[0] , and item_dictionary[1] corresponds to data[1] . 我有一个字典,其中item_dictionary[0]对应于data[0] ,而item_dictionary[1]对应于data[1]

item_dictionary = {'10350':'cat', '560':'dog'}
names = item_dictionary.values()

data = [[1, 2, 3, 4], [5, 6, 7]]

I tried to write the items in data[0] and data[1] to different .csv files like this: 我试图将data[0]data[1]的项目写入不同的.csv文件,如下所示:

def writer(item):
    q = data.index(item)    
    myfile = open('%r.csv', 'wb') % names[q]
    wr = csv.writer(myfile)
    wr.writerows(data[q])
    myfile.close()

z = [writer(x) for x in data]

This returns the error: 这将返回错误:

Traceback (most recent call last): 追溯(最近一次通话):
File "", line 1, in 文件“”,第1行,位于
File "", line 3, in writer 文件“”,第3行,在编写器中
TypeError: unsupported operand type(s) for %: 'file' and 'str'`. TypeError:%不支持的操作数类型:“文件”和“ STR”。

My guess for the 'str' part of the error is that names[q] returns, say, 'cat' instead of cat . 我对错误的'str'部分的猜测是, names[q]返回的是'cat'而不是cat However, I don't know what operand type to use instead and don't know how to get around the str problem, so could someone please help me? 但是,我不知道要使用哪种操作数类型,也不知道如何解决str问题,所以有人可以帮我吗? Thanks. 谢谢。

open('%r.csv', 'wb') % names[q] should be open('%r.csv' % names[q], 'wb') . open('%r.csv', 'wb') % names[q]应该是open('%r.csv' % names[q], 'wb')

In the former case you are opening a file named "%r.csv" for writing in binary mode and then attempting to modulo the file object with the contents of names[q] . 在前一种情况下,您将打开一个名为“%r.csv”的文件,以二进制方式进行写入,然后尝试使用names[q]的内容对文件对象取模。 file objects do not support the modulo operator and so you get the TypeError . file对象不支持取模运算符,因此您会收到TypeError

In the latter case we are invoking the format method of the string %r.csv with the module operator resulting in a string that is passed to the open function. 在后一种情况下,我们将使用模块运算符调用字符串%r.csvformat方法,从而将字符串传递给open函数。


As an aside, you may want to consider another way of associating names to data - you could store names alongside your data in either tuples, dictionaries or class instances: 顺便说一句,您可能想考虑将名称与数据相关联的另一种方法-您可以将名称与数据一起存储在元组,字典或类实例中:

# Tuples
data = [("cat", [1, 2, 3, 4]), ("dog", [5, 6, 7])]
for name, file_data in data:
    print name, "=>", file_data

# Dictionary (assumes only one instance of name per data point)
data = {"cat": [1, 2, 3, 4], "dog": [5, 6, 7]}
for name, file_data in data.items():
    print name, "=>", file_data

# Class instances (ignore this one if you haven't done any OO before)
# (You don't need it if you don't *know* you need it).
class MyDataType(object):
    def __init__(self, name, data):
        self.name = name
        self.data = data

data = [MyDataType("cat", [1, 2, 3, 4]), MyDataType("dog", [5, 6, 7])]
for data_point in data:
    print data_point.name, "=>", data_point.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM