[英]Python -> Organizing a List Text File
我是python的新用戶。 我有一個.txt格式(和.csv)的列表,像這樣
NEW YORK ....... from
31 Chatty, Seager Aarhaus
Atlas, Jones Abertham
Polly, Manning Antwerpen
Amazon, Brittle Belchental
LONDON ........ for
31 Park Dattemroed
Eleanor, Mallett Civeta Naples
3 Aurora Frigate Ljubljana
我想要
NEW YORK ....... from 31 Chatty, Seager Aarhaus
NEW YORK ....... from Atlas, Jones Abertham
NEW YORK ....... from Polly, Manning Antwerpen
NEW YORK ....... from Amazon, Brittle Belchental
LONDON ........ for 31 Park Dattemroed
LONDON ........ for Eleanor, Mallett Civeta Naples
LONDON ........ for 3 Aurora Frigate Ljubljana
我嘗試使用正則表達式,但無法獲得結果。
我想知道是否有辦法做到這一點。
這是一個打印所需輸出的程序:
with open('x.in') as input_file:
for line in input_file:
line = line.rstrip()
if '....' in line:
city = line
continue
print (city, line)
結果:
NEW YORK ....... from 31 Chatty, Seager Aarhaus
NEW YORK ....... from Atlas, Jones Abertham
NEW YORK ....... from Polly, Manning Antwerpen
NEW YORK ....... from Amazon, Brittle Belchental
LONDON ........ for 31 Park Dattemroed
LONDON ........ for Eleanor, Mallett Civeta Naples
LONDON ........ for 3 Aurora Frigate Ljubljana
如果城市線總是有.....
您可以使用groupby:
from itertools import groupby
with open(your_file) as f:
grps = groupby(f, key=lambda line: "......." in line)
for k,v in grps:
if k:
head = next(v).strip()
print("\n".join(["{} {}".format(head, line.strip()) for line in next(grps)[1]]))
這會給你:
NEW YORK ....... from 31 Chatty, Seager Aarhaus
NEW YORK ....... from Atlas, Jones Abertham
NEW YORK ....... from Polly, Manning Antwerpen
NEW YORK ....... from Amazon, Brittle Belchental
LONDON ........ for 31 Park Dattemroed
LONDON ........ for Eleanor, Mallett Civeta Naples
LONDON ........ for 3 Aurora Frigate Ljubljana
謝謝!
實際上,我試圖根據大寫單詞進行組織。 通過更改Padraic Cunningham代碼,我做到了
for line in Text:
newline = re.sub('^([A-Z][A-Z]+[A-Z])', '\\1≈', line)
≈只是顯示一個大寫單詞,然后
grps = groupby(f_, key=lambda line: "≈" in line)
for k,v in grps:
if k:
head = next(v).strip()
print('\n'.join(['{} {}'.format(head, line.strip()) for line in next(grps)[1]]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.