简体   繁体   English

将一行分成多行,但使用python重复名称

[英]break one line into multiple lines but repeating the names using python

I have an input file (fixed width format) text file like this: 我有一个输入文件(固定宽度格式)文本文件,如下所示:

id1|col2|col3|...|timestamp1,timestamp2,timestamp3,timestamp4,timestamp5  
id2|col2|col3|...|timestamp1,timestamp2  
id3|col2|col3|...|timestamp1  
...  

IDs have different numbers of timestamps. ID具有不同数量的时间戳。 The desired output should be structured as: each id should have one timestamp, 2nd timestamp should be on a different line with the same ids in front. 所需的输出结构应为:每个ID都应有一个时间戳,第二个时间戳应在不同的行上且具有相同的ID。

id1|col2|col3|...|timestamp1  
id1|col2|col3|...|timestamp2  
id1|col2|col3|...|timestamp3  
id1|col2|col3|...|timestamp4    
id1|col2|col3|...|timestamp5    
id2|col2|col3|...|timestamp1  
id2|col2|col3|...|timestamp2  
id3|col2|col3|...|timestamp1  

I have tried to read it in sql, but it is kind of tedious. 我试图在sql中阅读它,但这有点乏味。 I am looking to see if there is an answer in Python or unix. 我正在寻找在Python或UNIX中是否有答案。 Thanks a lot. 非常感谢。

You can use split('|') to cut your line using | 您可以使用split('|')使用|剪切行| delimiter, and then split(',') to cut the final field into a list of timestamps 定界符,然后split(',')将最后一个字段切成时间戳列表

with open('/path/to/file', 'r') as f:  
    for line in f:
        fields = line.split('|')
        timestamps = fields[-1].split(',')
        for timestamp in timestamps:
            print('|'.join(fields[:-1]+[timestamp]))

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

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