简体   繁体   English

Python - 使用列标题作为 csv 文件中字典的键(没有导入模块)

[英]Python - Using column headings as keys for dictionary from csv file (without import module)

I must perform some calculations with values provided from a.csv file, and need to use the column headings as keys to their values in each column.我必须使用 .csv 文件提供的值执行一些计算,并且需要使用列标题作为每列中它们的值的键。 However there is a likelihood that the columns of the file may be jumbled up or swapped around, and thus simply indexing for the values of each key wouldn't work.然而,文件的列有可能被打乱或交换,因此简单地为每个键的值建立索引是行不通的。 Also keeping in mind i cant import any modules such as csv. Heres a sample of what the csv file would look like, except with many more rows, and more AdultIDs...另外请记住,我无法导入任何模块,例如 csv。这是 csv 文件的示例,除了更多的行和更多的 AdultIDs...

AdultID成人ID Landmark地标 X X Y Z Z
R7033 R7033 Ex_L Ex_L -32 -32 -39 -39 -4.6 -4.6
R7033 R7033 En_L恩_L -1.8 -1.8 -41 -41 6.7 6.7
R7033 R7033 N 12 12 -34 -34 22.6 22.6
R7033 R7033 En_R恩_R 30.1 30.1 -43 -43 8.3 8.3

So effectively, I need the dictionary as such: {AdultID: [R7033, R7033, R7033], Landmark:[Ex_L, En_R, N, En_R]... } and so on.如此有效,我需要这样的字典:{AdultID: [R7033, R7033, R7033], Landmark:[Ex_L, En_R, N, En_R]... } 等等。

Assume that your input file (let's call it luh.csv) is a well formed CSV using commas as delimiters like this:假设您的输入文件(我们称之为 luh.csv)是一个格式正确的 CSV,使用逗号作为分隔符,如下所示:

AdultID,Landmark,X,Y,Z
R7033,Ex_L,-32,-39,-4.6
R7033,En_L,-1.8,-41,6.7
R7033,N,12,-34,22.6
R7033,En_R,30.1,-43,8.3

Then:然后:

with open('luh.csv') as csv:
    columns = next(csv).strip().split(',')
    dict_ = {}
    for line in map(str.strip, csv):
        for col, val in zip(columns, line.split(',')):
            dict_.setdefault(col, []).append(val)
    print(dict_)

Output: Output:

{'AdultID': ['R7033', 'R7033', 'R7033', 'R7033'], 'Landmark': ['Ex_L', 'En_L', 'N', 'En_R'], 'X': ['-32', '-1.8', '12', '30.1'], 'Y': ['-39', '-41', '-34', '-43'], 'Z': ['-4.6', '6.7', '22.6', '8.3']}

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

相关问题 Python - 如何使用列标题从CSV数据文件创建字典 - Python - How to Create Dictionary from CSV data file using column headings Python:不带CSV模块的CSV文件到字典 - Python: CSV File to Dictionary without csv module 带有列匹配的csv文件的Python字典键 - Python dictionary keys to csv file with column match 在不使用 python 模块的情况下从.csv 的列中找到最小值 - Finding minimum in a column from .csv without using python module 通过更改标题将.csv文件中的单个列拆分为多个列,并使用Python 2将其保存到新的.csv文件中 - Splitting a single column in a .csv file into multiple columns with changes in headings and saving it in a new .csv file using Python 2 将python Dictionary写入CSV文件,并将键作为列标题 - Writing a python Dictionary to a CSV file with keys as column headers python将带有行和列标题的csv文件读入带有两个键的字典中 - python read csv file with row and column headers into dictionary with two keys 在csv文件中排列字典中的键和值-Python - Arranging keys and values from a dictionary in a csv file - Python 不使用python csv模块将csv行(从for循环内)写出到csv文件 - write csv row (from within for loop) out to csv file without using python csv module 使用Python使用适当的列标题转换为CSV文件中的单行的列表列表 - List of lists converted to single row in CSV file with proper column headings using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM