I have a text file as follows and am trying to create a new text file. There is number on each string with a parenthesis. I need some help on how to parse this number.
test.txt
itemA (3)
itemB (test) abcd (2)
itemC xyx (3)
output.txt file to be created as:
itemA-1
itemA-2
itemA-3
itemB (test) abcd-1
itemB (test) abcd-2
itemC xyx-1
itemC xyx-2
itemC xyx-3
My current code:
import os
f = open('C:\\Dropbox\\test.txt','r')
data = f.read()
print (data)
f.close()
Easy enough with a regular expression:
That’ll match (optional) whitespace, followed by a number in parethesis, with the number put in a group for easy replacing:
Demo:
Complete code for your specific example: