简体   繁体   English

读取制表符分隔的文件,并将每一列分配给一个独立的变量

[英]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.

相关问题 python是否可以读取一列并为该列中的每一行分配一个单独的变量? - Is it possible for python to read a column and assign every row in that column a separate variable? 读取制表符分隔文件,第一列为键,其余为值 - Read a tab separated file with first column as key and the rest as values 在Python中使用制表符分隔的文件读取和导出单个列 - Read and export a single column from a tab-separated file in Python 从URL导入制表符分隔的表以生成熊猫数据框,并将每一列正确放入单独的列中 - Importing a tab-separated table from a url to produce a pandas dataframe with each column correctly put into separate columns 在由空格隔开的一行中为每个数据分配一个变量 - assign a variable to each data in a line separated by whitespaces 读取制表符分隔的txt文件并写入单独的列csv - Read a tab delimited txt file and write to separate column csv 从文件中读取每一行并分配给循环中的变量 - read each line from a file and assign to a variable in a loop 如何将列表的每个元素分配给一个单独的变量? - How to assign each element of a list to a separate variable? Python - 将列添加到现有制表符分隔文件中 - Python - add a column to an existing tab separated file Python-使用制表符分隔的单词将csv文件保存在单独的单元格中 - Python - save csv file with tab separated words in separate cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM