简体   繁体   English

如何使用列表标识键来打印未知数量的字典键?

[英]How can I print an unknown number of dictionary keys using a list to identify keys?

So I have searched and found certain things that have helped me put this code together, but I haven't had any luck with this last part. 因此,我已经搜索并找到了某些可以帮助我将这段代码组合在一起的东西,但是最后一部分我没有任何运气。 What I want to do is read in a file that is comma separated, space separated, or tab separated, set the headers as keys and the data as the values, and then write only certain columns (number of columns is unknown) to an output file. 我想做的是读取逗号分隔,空格分隔或制表符分隔的文件,将标题设置为键,将数据设置为值,然后仅将某些列(列数未知)写入输出中文件。 Example.txt is shown below: Example.txt如下所示:

col1, col2, col3, col4, col5
1, 11, 21, 31, 41
2, 12, 22, 32, 42
3, 13, 23, 33, 43
4, 14, 24, 34, 44

So far, here is the working code that I have so far. 到目前为止,这里是我到目前为止的工作代码。

import csv
import sys

file = sys.argv[1] # name of file is example.txt
columns = sys.argv[2:] # order: col1, col3, col5

with open(file, 'r') as csvfile:
    with open('table.out', 'w') as file_out:
        file.out_write(columns[0] + '\t' + columns[1] + '\t' + columns[2] + '\n')

        reader = csv.DictReader(csvfile)
        for row in reader:
            file_out.write(row[columns[0]] + '\t' + row[columns[1]] + '\t' + row[columns[2]] + '\n') 

Results: 结果:

col_1    col_3    col_5
1    21    41
2    22    42
3    23    43
4    24    44

This code works great if the number of columns were a fixed number, but the number of columns to be written can vary. 如果列数是固定数,则此代码非常有用,但是要写入的列数可以变化。 For example, sometimes I may want to only grab col1, col2 and other times I may want to grab col2, col3, col4, col5 in no particular order. 例如,有时我可能只想抓取col1,col2,而有时我可能想不按特定顺序抓取col2,col3,col4,col5。

So my question is, how can I modify the above code such that I can have any number of columns be written to the output file using dictionaries in Python 3.X? 所以我的问题是,如何修改以上代码,以便可以使用Python 3.X中的词典将任意数量的列写入输出文件?

You can adapt this to your needs, but basically using the join function will be really helpful + a list comprehension. 您可以根据自己的需要进行调整,但是基本上使用join函数将非常有帮助+列表理解。

import csv
import sys

file = sys.argv[1]
columns = sys.argv[2:]

with open(file) as f:
    myread = csv.DictReader(f)
    for row in myread:
        print('\t'.join([row[i] for i in columns]))
import csv
import sys

file = sys.argv[1] # name of file is example.txt
columns = sys.argv[2:] # order: col1, col3, col5
n_columns=len(columns)

with open(file, 'r') as csvfile:
    with open('table.out', 'w') as file_out:
        for i in range(0,n_columns):
            file_out.write(columns[i] + '\t')
        file_out.write('\n')

        reader = csv.DictReader(csvfile)
        for row in reader:
            for i in range(0,n_columns):
                file_out.write(row[columns[i]] + '\t')
            file_out.write('\n')

so, i've modified your code a little bit. 因此,我对您的代码做了一些修改。 to write a variable number of columns you can use a for statement that go from 0 to the length of the columns list. 要编写可变数量的列,您可以使用for语句,该语句从0到列列表的长度。

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

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