[英]Read multi line rows into a single row using python or python pandas
I have a data like this: 我有这样的数据:
MP|3561042|||WQTI544|BEA148|16077: POWER ID|7817|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16011: BINGHAM ID|45607|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16005: BANNOCK ID|82839|I|103306|||D|1
MP|3561250|||WQTI576
|BEA135|48301: LOVING TX|82|I|103308|||D|1
MP|3561250|||WQTI576
|BEA135|48443: TERRELL TX|984|I|103308|||D|1
MP|3561250|||WQTI576
|BEA135|48173: GLASSCOCK TX|1226|I|103308|||D|1
How do i achieve this: 我如何做到这一点:
MP|3561042|||WQTI544|BEA148|16077: POWER ID|7817|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16011: BINGHAM ID|45607|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16005: BANNOCK ID|82839|I|103306|||D|1
MP|3561250|||WQTI575|BEA135|48301: LOVING TX|82|I|103308|||D|1
MP|3561250|||WQTI576|BEA135|48443: TERRELL TX|984|I|103308|||D|1
MP|3561250|||WQTI576|BEA135|48173: GLASSCOCK TX|1226|I|103308|||D|1
I tried this: 我尝试了这个:
f=open('C:/Users/user/Desktop/a.csv','r')
lines=f.readlines()
mystr = '|'.join([line.strip() for line in lines])
print(mystr)
MP|3561042|||WQTI544|BEA148|16077: POWER,
ID|7817|I|103306|||D|1|MP|3561042|||WQTI544|BEA148|16011: BINGHAM,
ID|45607|I|103306|||D|1|MP|3561042|||WQTI544|BEA148|16005: BANNOCK,
ID|82839|I|103306|||D|1|MP|3561250|||WQTI576|||BEA135|48301: LOVING,
TX|82|I|103308|||D|1|MP|3561250|||WQTI576|||BEA135|48443: TERRELL,
TX|984|I|103308|||D|1|MP|3561250|||WQTI576|||BEA135|48173: GLASSCOCK,
TX|1226|I|103308|||D|1|MP|3561250|||WQTI576|
I am not achieving the way i want, any help please? 我没有达到我想要的方式,请帮忙吗? The first column always had a data of MP, and each row has 13 pipes as delimiters.
第一列始终具有MP数据,并且每一行都有13个管道作为分隔符。
Edited: 编辑:
How to do the same thing with the finding 'MP' instead of 'D|1', below is what i tried, but not giving the right thing cuz there are some rows that doesn't have the 'D|1' and has 'U|1234' something like that at the end 下面是我尝试过的与查找'MP'而不是'D | 1'相同的事情,但是我没有做正确的事情,因为有些行没有'D | 1'并且具有'U | 1234'像这样的结尾
content = ([l.strip().decode('utf-8') for l in s1 if l.strip()])
for line in content:
find_START = line.find('MP') # check if line has D|1
if find_START ==0:
tmp += line
res.append(tmp)
tmp = ''
else:
tmp += line
for r in res:
print(r)
Its printing as below: 其打印如下:
MP|3561042|||WQTI544|BEA148|16011: BINGHAM, ID|45607|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16005: BANNOCK, ID|82839|I|103306|||D|1
MP|3561250|||WQTI576
|BEA135|48301: LOVING, TX|82|I|103308|||D|1MP|3561250|||WQTI576
|BEA135|48443: TERRELL, TX|984|I|103308|||D|1MP|3561250|||WQTI576
|BEA135|48173: GLASSCOCK, TX|1226|I|103308|||D|1MP|3561250|||WQTI576
logFile : logFile :
MP|3561042|||WQTI544|BEA148|16077: POWER ID|7817|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16011: BINGHAM ID|45607|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16005: BANNOCK ID|82839|I|103306|||D|1
MP|3561250|||WQTI576
|BEA135|48301: LOVING TX|82|I|103308|||D|1
MP|3561250|||WQTI576
|BEA135|48443: TERRELL TX|984|I|103308|||D|1
MP|3561250|||WQTI576
|BEA135|48173: GLASSCOCK TX|1226|I|103308|||D|1
Hence : 因此 :
res = [] # empty list to store the results
tmp = '' # empty string for unindented lines
with open(logFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
find_END = line.find('D|1') # check if line has D|1
if find_END > 0:
tmp += line
res.append(tmp)
tmp = ''
else:
tmp += line
for r in res: print(r)
OUTPUT : 输出 :
MP|3561042|||WQTI544|BEA148|16077: POWER ID|7817|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16011: BINGHAM ID|45607|I|103306|||D|1
MP|3561042|||WQTI544|BEA148|16005: BANNOCK ID|82839|I|103306|||D|1
MP|3561250|||WQTI576|BEA135|48301: LOVING TX|82|I|103308|||D|1
MP|3561250|||WQTI576|BEA135|48443: TERRELL TX|984|I|103308|||D|1
MP|3561250|||WQTI576|BEA135|48173: GLASSCOCK TX|1226|I|103308|||D|1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.