在字典中制作字典以将数据按一列中的相同值分隔,然后从第二列中分隔

克洛特克诺

我是 Python 的新手,现在我被一个问题困住了几天。我做了一个脚本:

- 从 CSV 文件中获取数据 - 按数据文件第一列中的相同值对其进行排序 - 在不同模板文本文件的特定字段行中插入排序的数据 - 将文件保存在尽可能多的副本中,因为数据文件中的第一列中有不同的值下图显示了它是如何工作的:

在此处输入图像描述

But there are two more things I need to do. When in separate files as showed above, there are some of the same values from second column of the data file, then this file should insert value from third column instead of repeating the same value from second column. On the picture below I showed how it should look like:

在此处输入图像描述

What I also need is to add somewhere separeted value of first column from data file by "_".

There is datafile:

111_0,3005,QWE
111_0,3006,SDE
111_0,3006,LFR
111_1,3005,QWE
111_1,5345,JTR
112_0,3103,JPP
112_0,3343,PDK 
113_0,2137,TRE
113_0,2137,OMG

and there is code i made:

import shutil

with open("data.csv") as f:
    contents = f.read()
    contents = contents.splitlines()

values_per_baseline = dict()

for line in contents:
    key = line.split(',')[0]
    values = line.split(',')[1:]
    if key not in values_per_baseline:
        values_per_baseline[key] = []
    values_per_baseline[key].append(values)

for file in values_per_baseline.keys():
    x = 3
    shutil.copyfile("of.txt", (f"of_%s.txt" % file))
    filename = f"of_%s.txt" % file
    for values in values_per_baseline[file]:
        with open(filename, "r") as f:
            contents = f.readlines()
            contents.insert(x, '      o = ' + values[0] + '\n          ' + 'a = ' + values[1] +'\n')
        with open(filename, "w") as f:
            contents = "".join(contents)
            f.write(contents)
            f.close()

I have been trying to make something like a dictionary of dictionaries of lists but I can't implement it in correct way to make it works. Any help or suggestion will be much appreciated.

Timus

You could try the following:

import csv
from collections import defaultdict


values_per_baseline = defaultdict(lambda: defaultdict(list))
with open("data.csv", "r") as file:
    for key1, key2, value in csv.reader(file):
        values_per_baseline[key1][key2].append(value)

x = 3
for filekey, content in values_per_baseline.items():
    with open("of.txt", "r") as fin,\
         open(f"of_{filekey}.txt", "w") as fout:
        fout.writelines(next(fin) for _ in range(x))
        for key, values in content.items():
            fout.write(
                f'      o = {key}\n'
                + '          a = '
                + ' <COMMA> '.join(values)
                + '\n'
            )
        fout.writelines(fin)

输入读取部分使用csv标准库中的模块(为方便起见)和defaultdict. 该文件被读入嵌套字典。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在我的数据框中的一列中分隔类别

在R中的多列中分隔一列

将python字典写入CSV列:第一列的键,第二列的值

按唯一列值分隔数据

将两列分组:第一列作为字典,键作为第一列值,第二列作为字典值

如何在Python中基于第一列的值从csv文件中分隔行

熊猫按第一列分组,并从第二列添加逗号分隔的条目

字典数据未分隔为Pandas DataFrame中的列

如何根据另一列中的逗号分隔值按名称引用列?

根据数据框中两列之间的差异对字典的值求和,并将第一列除以二 - Python

将一列中的值与另一列中的逗号分隔值匹配并返回匹配的值

将一列中的值分隔为SSMS中的不同段或列

在数据框中搜索值,然后将字典中的相应值放入新列中

从与另一列中的某些值对应的列中获取值,直到在第二列中获得相同的值

获取由R数据框中的另一列中的值分隔的一列的箱线图统计信息

当键的值在另一列中时,将字典的键放在DataFrame的列中

查找一列的最大值(按组),然后将值插入R中的另一数据帧

Pandas DataFrame read_csv 第一列作为字典中的键,第二列作为值(以简单的方式)

将一列中的值与第二个数据帧中一列中的值进行比较

Google 表格 - 将一列中未排序的逗号分隔值解析为单独的列。值并不总是以相同的顺序

分隔R中的一列

无法在由另一列值分组的逗号分隔的单行中获取数据

使用字典参考另一列值来映射熊猫数据框的一列中的缺失值

如何将逗号分隔的数据拆分到同一列中

将一列相加,然后减去第二列

根据第一列内容分隔字段,在第二列中匹配,在awk中第四列中减去

从值从字典列表的另一列中删除项目

对第一列中的值进行计数,其中第二列的出现在UNIX中是相同的

使用不同的分隔符将一列中的值拆分为 SQL 中的多列