简体   繁体   English

在python中将csv文件导入为字典以稍后计算面积值

[英]Importing a csv file as dictionary in python to later calculate areal values

I have the following problem. 我有以下问题。 I have a .csv files (180 entries) which looks like this: 我有一个.csv文件(180个条目),看起来像这样:

Codes,Area
3443,0
3530,0
3583,0
3589,0
3514,0

I read that .csv-file into python using this command: 我使用以下命令将.csv文件读入python:

input_file = csv.DictReader(open("CPC.csv"))
for row in input_file:
    print row

The dictionary then looks like this: 然后,字典如下所示:

{'Codes': '3443', 'Area': '0'}
{'Codes': '3530', 'Area': '0'}
{'Codes': '3583', 'Area': '0'}
{'Codes': '3589', 'Area': '0'}
{'Codes': '3514', 'Area': '0'}

That looks pretty and I really like it. 看起来很漂亮,我真的很喜欢。 My aim is to calculate area values in m^2 for every code. 我的目的是为每个代码计算m ^ 2中的面积值。 Therefore this dictionary is just temporary to sum up all areas for their specific codes and to later then take that value and re-assign it to a new table. 因此,此字典只是临时的工作,目的是汇总所有区域的特定代码,然后稍后再使用该值并将其重新分配给新表。 I have however a problem: how do I access my codes? 但是,我有一个问题:如何访问我的代码? Therefore I just wanted to have actually this type of dictionary: 因此,我只想拥有这种类型的字典:

{'3443', '0'}
{'3530', '0'}
{'3583', '0'}
{'3589', '0'}
{'3514', '0'}

So that I just need to search for my code (like for example 3443 and that I then can add to the 0 which is there my 125m^2 which are in my other table. I fail to see how to do it. Anybody could help please? 因此,我只需要搜索我的代码(例如3443,然后我可以将其添加到0,这就是我的另一个表中的125m ^ 2。我看不到如何做。任何人都可以帮助请?

Your original code reads the csv file, parses the format to split the columns, then prints each parsed row. 您的原始代码读取csv文件,解析格式以拆分列,然后打印每个解析的行。 It doesn't store the objects in memory. 它不将对象存储在内存中。

You have to choose if you want to store them all in memory, or you want to process them as you go and store the results somewhere else, like, in another file. 您必须选择是将它们全部存储在内存中,还是要随时处理它们并将结果存储在其他文件中,例如另一个文件中。

From your problem description it seems you will deal with each code separately. 从问题描述来看,您似乎将分别处理每个代码。 However you didn't provide your "other table" so I don't know exactly how it is structured. 但是,您没有提供“其他表”,因此我不完全知道它的结构。

The example code below will read the file, code by code, and for each code it will search YOUR_OTHER_TABLE for the value to put in the Area field. 下面的示例代码将逐个代码读取文件,对于每个代码,它将在YOUR_OTHER_TABLE中搜索要放入Area字段的值。 Then it will write each row to a separate result file. 然后它将每一行写入单独的结果文件。

Also, it seems you're using python 2, so the example below is also using python 2 specifics (for example, opening csv files in binary mode, in python 3 you don't want that): 另外,似乎您正在使用python 2,因此以下示例也使用python 2规范(例如,以二进制模式打开csv文件,而在python 3中则不希望这样做):

with open("CPC.csv", 'rb') as f, open('CPC_result.csv', 'wb') as fw:
    csv_f = csv.DictReader(f)
    csv_w = csv.DictWriter(fw, csv_f.fieldnames)
    for row in csv_f:
        code = row['Codes']
        calculated_area = YOUR_OTHER_TABLE.get(code, 0)
        row['Area'] = str(calculated_area)
        csv_w.writerow(row)
areas = {}
input_file = csv.reader(open("CPC.csv"))
next(input_file)
for row in input_file:
    key, val = row
    areas[key] = val

print areas
#{'3443': '0', '3530': '0', '3583': '0', '3589': '0', '3514': '0'}

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

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