Reputation: 844
I have a file of many line. of format as below,
//many lines of normal text
00.0000125 1319280 9.2 The Shawshank Redemption (1994)
//lines of text
0000011111 59 6.8 "$#*! My Dad Says" (2010) {You Can't Handle the Truce (#1.10)}
1...101002 17 6.6 "$1,000,000 Chance of a Lifetime" (1986)
I want to split the data as columns 1...101002,17,6.6,"$1,000,000 Chance of a Lifetime" (1986)
The program I tried is ,
import re
f = open("E:/file.list");
reg = re.compile('[+ ].{10,}[+ ][+0-9].{3,}[+ ]')
for each in f:
if reg.match(each):
print each
print reg.split(each)
It is not giving correct answer can I know the regex to use.
Upvotes: 0
Views: 70
Reputation: 107347
First you split the lines by split()
function then slice the split list (use itertools.islice()
)from leading of list to where that you have a number in parenthesis (if re.match(r'\(\d+\)',j)
) :
>>> s="""0000011111 59 6.8 "$#*! My Dad Says" (2010) {You Can't Handle the Truce (#1.10)}"""
>>> s.split()
['0000011111', '59', '6.8', '"$#*!', 'My', 'Dad', 'Says"', '(2010)', '{You', "Can't", 'Handle', 'the', 'Truce', '(#1.10)}']
>>> l=s.split()
>>> [list(islice(l,0,i+1)) for i,j in enumerate(l) if re.match(r'\(\d+\)',j)]
[['0000011111', '59', '6.8', '"$#*!', 'My', 'Dad', 'Says"', '(2010)']]
If you have your lines in a list (read the file with readlines()
) :
>>> lines = ["""00.0000125 1319280 9.2 The Shawshank Redemption (1994)""","""0000011111 59 6.8 "$#*! My Dad Says" (2010) {You Can't Handle the Truce (#1.10)}""", """1...101002 17 6.6 "$1,000,000 Chance of a Lifetime" (1986)"""]
>>> [list(islice(line.split(),0,i+1)) for line in lines for i,j in enumerate(line.split()) if re.match(r'\(\d+\)',j)]
[['00.0000125', '1319280', '9.2', 'The', 'Shawshank', 'Redemption', '(1994)'], ['0000011111', '59', '6.8', '"$#*!', 'My', 'Dad', 'Says"', '(2010)'], ['1...101002', '17', '6.6', '"$1,000,000', 'Chance', 'of', 'a', 'Lifetime"', '(1986)']]
Upvotes: 1
Reputation: 67978
It is easier to match instead of split in this case.
^\s*(\S+)\s+(\S+)\s+(\S+)\s+(.*)$
Try this.See demo.
http://regex101.com/r/oE6jJ1/47
import re
p = re.compile(ur'^\s*(\S+)\s+(\S+)\s+(\S+)\s+(.*)$', re.IGNORECASE | re.MULTILINE)
test_str = u"00.0000125 1319280 9.2 The Shawshank Redemption (1994)\n\n 0000011111 59 6.8 \"$#*! My Dad Says\" (2010) {You Can't Handle the Truce (#1.10)}\n 1...101002 17 6.6 \"$1,000,000 Chance of a Lifetime\" (1986)"
re.findall(p, test_str)
Upvotes: 1
Reputation: 26667
What about something like
>>> str='1...101002 17 6.6 "$1,000,000 Chance of a Lifetime" (1986)'
>>> re.findall(r'^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)', str)
[('1...101002', '17', '6.6', '"$1,000,000 Chance of a Lifetime" (1986)')]
Upvotes: 1
Reputation: 5306
I changed RegEx pattern.
import re
f = open("file.txt");
reg = re.compile(r" (.{10}) *(\d*) *(\d*\.\d*) (.*)")
for each in f:
if reg.match(each):
print each
print reg.split(each)
Upvotes: 1
Reputation: 8709
>>> text="""0000011111 59 6.8 "$#*! My Dad Says" (2010) {You Can't Handle the Truce (#1.10)}
... 1...101002 17 6.6 "$1,000,000 Chance of a Lifetime" (1986)"""
>>> re.findall(r'([0-9\.]+)\s*([0-9]+)\s*([0-9\.]+)\s*(".*")',text)
[('0000011111', '59', '6.8', '"$#*! My Dad Says"'), ('1...101002', '17', '6.6', '"$1,000,000 Chance of a Lifetime"')]
Upvotes: 1