[英]Sort with re.search() - Python
我在解决以下问题时遇到了一些问题。
我必须 *.txt 文件中的两个文件都是来自奥地利的城市。 在第一个文件“cities1”中,城市按人口排序。
第一个文件 (cities1.txt) 如下所示:
1.,Vienna,Vienna,1.840.573
2.,Graz,Styria,273.838
3.,Linz,Upper Austria,198.181
4.,Salzburg,Salzburg,148.420
5.,Innsbruck,Tyrol,126.851
第二个文件 (cities2.txt) 如下所示:
"Villach","Carinthia",60480,134.98,501
"Innsbruck","Tyrol",126851,104.91,574
"Graz","Styria",273838,127.57,353
"Dornbirn","Vorarlberg",47420,120.93,437
"Vienna","Vienna",1840573,414.78,151
"Linz","Upper Austria",198181,95.99,266
"Klagenfurt am Woerthersee","Carinthia",97827,120.12,446
"Salzburg","Salzburg",148420,65.65,424
"Wels","Upper Austria",59853,45.92,317
"Sankt Poelten","Lower Austria",52716,108.44,267
我喜欢做的,或者换句话说我应该做的是,第一个文件cities1.txt
已经排序。 我只需要每行的第二个元素。 这意味着我只需要城市的名称。 例如从第2.,Graz,Styria,273.838
,我只需要 Graz。
第二个我应该打印出城市的区域,这是cities2.txt
中每一行的第四个元素。 这意味着,例如从第三行"Graz","Styria",273838,127.57,353
,我只需要127.57
。
最后,控制台应显示以下内容:
Vienna,414.78
Graz,127.57
Linz,95.99
Salzburg,65.65
Innsbruck,104.91
所以,我现在的问题是,如果我只允许使用re.search()
方法,我该怎么做。 因为第二个 *.txt 文件的顺序不同,我必须按照与第一个文件中相同的顺序来排列城市,这样才能正常工作,或者?
我知道,使用re.split()
会容易得多,因为您无法比较两个文件中的列表元素。 但我不允许这样做。
我希望有人可以帮助我,并对长文本感到抱歉。
这是基于我之前评论的实现:
with open('cities2.txt') as c:
D = {}
for line in c:
t = line.split(',')
cn = t[0].strip('"')
D[cn] = t[-2]
with open('cities1.txt') as d:
for line in d:
t = line.split(',')
print(f'{t[1]},{D[t[1]]}')
请注意,这可能并不可靠。 如果 city1.txt 中的城市名称在 cities2.txt 中不存在,那么您将收到 KeyError
这只是一个提示,毕竟这是你的大学作业。
import re
TEST = '2.,Graz,Styria,273.838'
RE = re.compile('^[^,]*,([^,]*),')
if match := RE.search(TEST):
print(match.group(1)) # 'Graz'
让我们分解正则表达式:
^ - start of line
[^,]* - any character except a comma - repeated 0 or more times
this is the first field
, - one comma character
this is the field separator
( - start capturing, we are interested in this field
[^,]* - any character except a comma - repeated 0 or more times
this is the second field
) - stop capturing
, - one comma character
(don't care about the rest of line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.