简体   繁体   中英

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. In the first file "cities1" are the cities are ordered by population.

The first file (cities1.txt) is looking like this:

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:

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

Than second i should print out the area of the city, this is the fourth element of every line in cities2.txt . That means, for example from the third line "Graz","Styria",273838,127.57,353 , i only need 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. 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?

I know, it would be much easier to use re.split() because than you are able to compare the list elements form both files. 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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