regex a line from file using python -
I am trying to regex data from a row in the file and fail. I tried to use but failed there too. The code I came up with is:
#! / Usr / bin / python3 # To be parsed line: # 1. Ni type = 1 np = 1001 r1 = 1.0 e-0 05 rnp = -1.35602175 pfile = Ni1.pot # parse # 1 ni 1.pot import again f1 = open ("file. Arstric", "r") Pattern = R '(\ D +) \ s + (. *) \ S + type = (\ D +) * \ s pfile = (. *)' Count = []; Atype = []; Boxes = [] for f1 in line: match = re-search (pattern, line) if match: count.append (int (match.group (1))) atype.append ((match.group (2))) apots .append ((match.group (3))) Print (count) Does anyone show me what error I have?
You may want to change the pattern like this
pattern = R '(\ d +) \. * S * (. *) \ S + type = (\ d +) * Pfile = (. *) ' Because, there is a dot after the first number, so we have
(\ d +) \ There may then be more white space characters,
(\ d +) \. * S * > Then we use the word, ny (\ d +) \ * S * (. *) capture and then can be 1 or more spaces, followed by type = & lt; Numbers> (\ d +) \. * S * (. *) \ S + followed by pfile = & lt; Filename & gt; After , there may be any letter, type = (\ d +) we call it
(\ d +) \ * S * (. *) \ S + type = (\ d +) * Pfile = (. *) And since you are capturing four objects,
count, atype, pots, files = [] Files.append (match.group (4)) print (calculation, encryption, pots, files) # [1], [ 'Ni'], ['1'], ['Ni1.pot']
Comments
Post a Comment