0

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()
1
  • 1
    To format code for stackoverflow, just paste it in verbatim and press Ctrl+K. Thanks! Commented Sep 6, 2012 at 19:58

2 Answers 2

3

Easy enough with a regular expression:

r'\s*\((\d+)\)'

That'll match (optional) whitespace, followed by a number in parethesis, with the number put in a group for easy replacing:

parensnumber = re.compile(r'\s*\((\d+)\)')

count = int(parensnumber.search(line).group(1))
for i in range(count):
    print(parensnumber.sub('-{0}'.format(i + 1), line))

Demo:

>>> import re
>>> parensnumber = re.compile(r'\s*\((\d+)\)')
>>> parensnumber.sub(r'-\1', 'itemA (3)')
'itemA-3'
>>> parensnumber.sub(r'-\1',  'itemB (test) abcd (2)')
'itemB (test) abcd-2'
>>> line = 'itemB (test) abcd (2)'
>>> count = int(parensnumber.search(line).group(1))
>>> for i in range(count):
...     print(parensnumber.sub('-{0}'.format(i + 1), line))
... 
itemB (test) abcd-1
itemB (test) abcd-2

Complete code for your specific example:

import os
import re

parensnumber = re.compile(r'\s*\((\d+)\)')

with open('C:\\Dropbox\\test.txt','r') as input:
    for line in input:
        count = int(parensnumber.search(line).group(1))
        for i in range(count):
            print(parensnumber.sub('-{0}'.format(i + 1), line))
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

o = open('output.txt', 'w')
for line in open('text.txt'):
    vals = line.split(' ')
    n = int(vals[-1][1:-1])
    for i in range(n):
        o.write("%s-%d" % (" ".join(vals[:-1]), i))
o.close()

Provided your INPUT is correct. Although you might want to consider using REGEXPs instead of parsing it "by hand".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.