简体   繁体   English

使用 re.search() 排序 - Python

[英]Sort with re.search() - Python

i have some problems with solving the follwoing problem.我在解决以下问题时遇到了一些问题。

I have to *.txt files in both files are cities from austria.我必须 *.txt 文件中的两个文件都是来自奥地利的城市。 In the first file "cities1" are the cities are ordered by population.在第一个文件“cities1”中,城市按人口排序。

The first file (cities1.txt) is looking like this:第一个文件 (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

The second file (cities2.txt) is looking like this:第二个文件 (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

What i like to do, or in other words what i should do is, the first file cities1.txt is already sorted.我喜欢做的,或者换句话说我应该做的是,第一个文件cities1.txt已经排序。 I only need the second element of every line.我只需要每行的第二个元素。 That means i only need the name of the city.这意味着我只需要城市的名称。 For example from the line 2.,Graz,Styria,273.838 , i only need Graz.例如从第2.,Graz,Styria,273.838 ,我只需要 Graz。

Than second i should print out the area of the city, this is the fourth element of every line in cities2.txt .第二个我应该打印出城市的区域,这是cities2.txt中每一行的第四个元素。 That means, for example from the third line "Graz","Styria",273838,127.57,353 , i only need 127.57 .这意味着,例如从第三行"Graz","Styria",273838,127.57,353 ,我只需要127.57

At the end the console should display the following:最后,控制台应显示以下内容:

Vienna,414.78
Graz,127.57
Linz,95.99
Salzburg,65.65
Innsbruck,104.91

So, my problem now is, how can i do this, if i only allowed to use the re.search() method.所以,我现在的问题是,如果我只允许使用re.search()方法,我该怎么做。 Cause the second *.txt file is not in the same order and i have to bring the cities in the same order as in the first file that this will work, or?因为第二个 *.txt 文件的顺序不同,我必须按照与第一个文件中相同的顺序来排列城市,这样才能正常工作,或者?

I know, it would be much easier to use re.split() because than you are able to compare the list elements form both files.我知道,使用re.split()会容易得多,因为您无法比较两个文件中的列表元素。 But I'm not allowed to do this.但我不允许这样做。

I hope someone can help me and sorry for the long text.我希望有人可以帮助我,并对长文本感到抱歉。

Here's an implementation based on my earlier comment:这是基于我之前评论的实现:

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]]}')

Note that this may not be robust.请注意,这可能并不可靠。 If there's a city name in cities1.txt that does not exist in cities2.txt then you'll get a KeyError如果 city1.txt 中的城市名称在 cities2.txt 中不存在,那么您将收到 KeyError

This is just a hint, it's your university assignment after all.这只是一个提示,毕竟这是你的大学作业。

import re

TEST = '2.,Graz,Styria,273.838'

RE = re.compile('^[^,]*,([^,]*),')

if match := RE.search(TEST):
    print(match.group(1)) # 'Graz'

Let's break down the regexp:让我们分解正则表达式:

 ^      - 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.

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