[英]Read a Tab Separated File and Assign Each Column to a Separate Variable
The following python code reads a tab separated file which contains multiple columns. 以下python代码读取一个制表符分隔的文件,其中包含多列。 I have store each column in a separate variable then tried to store the column into a dictionary and print out the the values of the dictionary.
我已经将每个列存储在一个单独的变量中,然后尝试将该列存储到字典中并打印出字典的值。
import csv
dic1={}
dic2={}
with open("Table.tsv") as samplefile:
reader = csv.reader(samplefile, delimiter="\t")
columns = zip(*reader)
for column in columns:
A, B, C, D = columns #store the columns into separate variables
dic1[A] = samplefile # storing a specific variable (column) into a dictionary
print (dic1[A])
Problem: I am not able to print out the dictionary which contains the data of "A" column! 问题:我无法打印出包含“ A”列数据的词典! Not sure how to solve this problem.
不确定如何解决此问题。
Error message: <closed file 'Table.tsv', mode 'r' at 0x7fef50ba0030>
错误消息:
<closed file 'Table.tsv', mode 'r' at 0x7fef50ba0030>
Your help is appreciated, 感谢您的帮助,
In the following line 在下一行
dic1[A] = samplefile
you are assigning the file object named samplefile
to the dictionary and are using the content of variable 'A' as key value. 您正在将名为
samplefile
的文件对象分配给字典,并使用变量“ A”的内容作为键值。 And you error message is not an error message, its python's string representation of the file object. 而且您的错误消息不是错误消息,它是python的文件对象的字符串表示形式。 The file object is closed, since you left the
with
context. 由于您留下了
with
上下文,因此文件对象已关闭。
You have to actually assign the variables to the dictionary like so. 您实际上必须像这样将变量分配给字典。
dic1['a'] = A
Maybe you want to reread some information about how dictionaries work. 也许您想重新阅读一些有关字典工作方式的信息。 Have a look here: http://learnpythonthehardway.org/book/ex39.html
在这里看看: http : //learnpythonthehardway.org/book/ex39.html
Others have explained the problem that you are seeing, but a better way of writing your code is to use csv.DictReader
. 其他人已经解释了您所看到的问题,但是编写代码的更好方法是使用
csv.DictReader
。
import csv
with open("Table.tsv") as samplefile:
reader = csv.DictReader(samplefile, delimiter="\t")
for row in reader:
print row['A']
The above assumes that the first line of your csv file contains the column names. 以上假设您的csv文件的第一行包含列名称。 If not, pass a list of the column names to
DictReader
: 如果不是,请将列名称列表传递给
DictReader
:
reader = csv.DictReader(samplefile, ['A', 'B', 'C', 'D'], delimiter="\t")
Python cares about indentation (understatement of the year!). Python关心缩进(一年中的低估!)。 You need to indent your "for in" code block once more to make it part of the "within" block.
您需要再次缩进“ for in”代码块,以使其成为“ inin”块的一部分。
You aren't getting an error message. 您没有收到错误消息。
with open("Table.tsv") as samplefile:
..
dic1[A] = samplefile # storing a specific variable (column) into a dictionary
print (dic1[A])
You open a file, put the file handle in a dictionary, then take it out and print it. 打开文件,将文件句柄放入字典中,然后将其取出并打印。 Python prints it and you see the text representation of a closed file handle .
Python将其打印出来,您会看到一个关闭的文件句柄的文本表示形式 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.