How to split Strings of a text file into char and number in python -


i'm having file contains strings :

n1109 x62.729 y23.764 z231.442 a59.756 b9.231 

so want split char , integers file. output should me :

n 1109  x 62.729 y 23.764 z 231.442  59.756 b 9.231 

this in text file. dont know how text file.

code i've wrote :

import re sys import argv script, filename = argv f = open(filename,"r") lines = f.readlines() print lines r = re.compile("([a-za-z]+)([0-9]+)") = [r.match(string).group() string in lines] print 

when use group() got error :

`attributeerror: 'nonetype' object has no attribute 'group'` 

and when remove group() output :

[<_sre.sre_match object @ 0xb72f1b18>, none, none, none, none, none, none, none, none, none, none]

please me i'm new in python...

you can use re module purpose.

try this, may you.

import re >>> match = re.match(r"([a-z]+)([0-9]+)", 'n1109', re.i) >>> if match:         print match.groups()  output:  ('n', '1109') 

update

>>> a=['n1109', 'x62.729', 'y23.764', 'z231.442', 'a59.756', 'b9.231'] >>> answer=[] >>> in a:         match = re.match(r"([a-z]+)([0-9]*\.?[0-9]+)", i, re.i)             if match:                    answer.append(match.groups())   >>> answer [('n', '1109'), ('x', '62.729'), ('y', '23.764'), ('z', '231.442'), ('a', '59.756'), ('b', '9.231')] >>>  

update

>>> open(r'd:\test1.txt') f:          content = f.readlines()        >>> content=' '.join(content) >>> content=content.split() >>> answer=[] >>> in content:          match = re.match(r"([a-z]+)([0-9]*\.?[0-9]+)", i, re.i)                 if match:                     answer.append(match.groups())   >>> answer [('n', '1100'), ('x', '63.658'), ('y', '21.066'), ('z', '230.989'), ('a', '60.28'), ('b', '9.5'), ('n', '1101'), ('x', '63.424'), ('y', '21.419'), ('z', '231.06'), ('a', '60.269'), ('b', '9.459'), ('n', '1102'), ('x', '63.219'), ('y', '21.805'), ('z', '231.132'), ('a', '60.231'), ('b', '9.418'), ('n', '1103'), ('x', '63.051'), ('y', '22.206'), ('z', '231.202'), ('a', '60.169'), ('b', '9.377'), ('n', '1104'), ('x', '62.915'), ('y', '22.63'), ('z', '231.272'), ('a', '60.083'), ('b', '9.335'), ('n', '1105'), ('x', '62.863'), ('y', '22.851'), ('z', '231.307'), ('a', '60.027'), ('b', '9.314'), ('n', '1106'), ('x', '62.811'), ('y', '23.073'), ('z', '231.341'), ('a', '59.971'), ('b', '9.293'), ('n', '1111'), ('x', '62.702'), ('y', '24.227'), ('z', '231.506'), ('a', '59.596'), ('b', '9.191'), ('n', '1112'), ('x', '62.71'), ('y', '24.462'), ('z', '231.536'), ('a', '59.503'), ('b', '9.172'), ('n', '1113'), ('x', '62.718'), ('y', '24.697'), ('z', '231.567'), ('a', '59.41'), ('b', '9.152'), ('n', '1114'), ('x', '62.727'), ('y', '24.932'), ('z', '231.597'), ('a', '59.316'), ('b', '9.133'), ('n', '1115'), ('x', '62.734'), ('y', '25.167'), ('z', '231.627'), ('a', '59.222'), ('b', '9.114'), ('n', '1123'), ('x', '62.793'), ('y', '27.037'), ('z', '231.864'), ('a', '58.46'), ('b', '8.961')] >>>  

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -