简体   繁体   English

如何使用管道定界符将值附加到python中的相同键

[英]How to append value to the same key in python using pipe delimiter

I'm reading a text file which I will convert into array of (key,pairs). 我正在阅读一个文本文件,它将转换为(key,pairs)数组。 I want to append the values if I have same key with pipe delimiter. 如果我具有与管道定界符相同的键,我想附加值。 I'm new to python, can someone please help me to fix this issue. 我是python的新手,有人可以帮助我解决此问题。 Please find my code and input and output format: 请找到我的代码以及输入和输出格式:

File Input: (test.txt) 文件输入:(test.txt)

1234567 E29dceffb-28bd-4f05-b91f-03f05510c081,0743,438864,10/06/2016,7,IK,PP,7136977,,4,37791,,,,01/11/2017
83738383 E29dceffb-38373-3839-8393-83038383,0743,438864,10/06/2016,7,PO,VC,4,37791,,,,01/11/2017
1234567 E29dceffb-38DFG3-4f05-0393-39303933,1234,78789,10/06/2016,7,LL,YY,7136977,,4,37791,,,01/10/2017

Python code: Python代码:

import sys

with open('test.txt') as f:
  result = dict(line.split(' ', 1) for line in f)
  for line in list:
    if line[0] in result:
        result[line[0]].append(line[1])
    else:
        result[line[0]] = [line[1]]

I want to print my key and value like below: 我想打印我的键和值,如下所示:

Key: 1234567.AB.K
Value: E29dceffb-28bd-4f05-b91f-03f05510c081,0743,438864,10/06/2016,7,IK,PP,7136977,,4,37791,,,,01/11/2017|E29dceffb-38DFG3-4f05-0393-39303933,1234,78789,10/06/2016,7,LL,YY,7136977,,4,37791,,,01/10/2017|

Key: 83738383.AB.K
Value: E29dceffb-38373-3839-8393-83038383,0743,438864,10/06/2016,7,PO,VC,4,37791,,,,01/11/2017|

Using pipe delimiter if more than one value occurs for same key to differentiate first record, second record and so on. 如果同一键出现多个值,则使用管道定界符来区分第一条记录,第二条记录等。

I'll be getting around 2 millions of entry in test.txt file. 我将在test.txt文件中获得大约200万个条目。

How efficiently I can handle this in python. 我在python中处理此问题的效率如何。 My python script is not working as expected its breaking the code if i try to append the value for same key. 我的python脚本无法按预期工作,如果我尝试为同一键附加值,则会破坏代码。

Any help is very much appreciated! 很感谢任何形式的帮助! Thanks in advance. 提前致谢。

I'm not sure about your list on that for loop. 我不确定您在该for循环中的list

The reason your code break when you try to append the value for same key is because you create result as dictionary with each value being a single string. 尝试为同一键附加值时代码中断的原因是,您将result创建为字典,每个值都是单个字符串。

What you need is to create a result_dict for your final result, something like this 您需要为最终结果创建一个result_dict ,类似这样

result_dict = {}

with open('test.txt') as f:
  lines = list(line.split(' ', 1) for line in f)
  for line in lines:
    if line[0] in result_dict:
        result_dict[line[0]].append(line[1].strip())
    else:
        result_dict[line[0]] = [line[1].strip()]

print(result_dict)

will print 将打印

{'1234567.AB.K': ['E29dceffb-28bd-4f05-b91f-03f05510c081,0743,438864,10/06/2016,7,IK,PP,7136977,,4,37791,,,,01/11/2017', 'E29dceffb-38DFG3-4f05-0393-39303933,1234,78789,10/06/2016,7,LL,YY,7136977,,4,37791,,,01/10/2017'], '83738383.AB.K': ['E29dceffb-38373-3839-8393-83038383,0743,438864,10/06/2016,7,PO,VC,4,37791,,,,01/11/2017']}

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

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