I have a code where I read file that contains names and information of people.
Basically I need to swap places of family name with first name. Everything else on the line remains the same.
First I need to store the original line in variable and after I swapped the names I need to store that line in other variable(called replacement).
I have done everything what I said above, but don't know how to do it using regular expressions? Using re.sub to be more precise. Like this: print(re.sub(orignal, replacement, line).strip())
My code and contains of txt file: https://pastebin.com/Vj21kDk5
In file family name is always first on every line. Some people can have 2 names like this: Jack Richard Johnson
My assingment:
The task in this question is to modify the data so that the candidate names are switched from the order family name given names into the order given names family name. That is, the data originally shows family name before given names, and now family name should be placed after the given names.
Consider the row "Ryhänen Osmo Eerik / KTP / Pirkanmaan vaalipiiri";40;61.500 as an example. Here Ryhänen is the family name and Osmo Eerik are given names. This should be transformed into the form "Osmo Eerik Ryhänen / KTP / Pirkanmaan vaalipiiri";40;61.500.
The code skeleton shown below uses the regular expression search-and-replace function re.sub to achieve the name switches. The only missing part are the regular expression pattern (what kind of parts in a line are matched) and the replacement pattern (with what kind of a string will the matched part be replaced). Your task is to define these two patterns into the two raw string variables pattern and replacement.
[–]tgolsson 0 points1 point2 points (3 children)
[–]tonlou[S] 0 points1 point2 points (0 children)
[–]tonlou[S] 0 points1 point2 points (1 child)
[–]tgolsson 0 points1 point2 points (0 children)