I have the following problem: I have a .txt file which contains information in the following scheme:
ddddd name country time dddd name country time
The ddddd stands for 5 digits, representing the ID number of the participant. With available information I have come so far:
filename = 'raw_race.txt'
new_filename = 'data_race.txt'
pattern = re.compile(r'\D(\d{5})\D')
with open(filename, 'r') as readfile, open(new_filename, 'w+') as writefile:
for line in readfile:
writefile.write(line)
if pattern.search(line):
writefile.write('\n')
This does not work (output file is the same as input) and also, this would add the paragraph after the pattern, not before.
Does anyone see what I can do? Cheers!
for line in readfile:
writefile.write(re.sub(r'\b(\d{5})\b', '\n\\1', line))
did the trick, thank you!
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.