regex - Regular Expressions to Update a Text File in Python -


i'm trying write script update text file replacing instances of characters, (i.e. 'a', 'w') word (i.e. 'airplane', 'worm').

if single line of text this:

a.function(); a.callmethod(w); e.aa(w); 

i'd want become this:

airplane.function(); airplane.callmethod(worm); e.aa(worm); 

the difference subtle important, i'm changing 'a' , 'w' it's used variable, not character in other word. , there's many lines in file. here's i've done far:

original = open('original.js', 'r') modified = open('modified.js', 'w') # iterate through each line of file line in original:     # search character 'a' when not part of word of sort     line = re.sub(r'\w(a)\w', 'airplane', line)     modified.write(line)  original.close() modified.close() 

i think re pattern wrong, , think i'm using re.sub() method incorrectly well. appreciated.

if you're concerned semantic meaning of text you're changing regular expression, you'd better served parsing instead. luckily python has 2 modules parsing python. @ abstract syntax tree , parser modules. there's others javascript if that's you're doing; slimit.

future reference on regular expression questions, there's lot of helpful information here:

and took me 30 minutes never having used javascript parser in python (replete installation issues: please note right ply version) writing basic solution given example. can too.

# note: sudo pip3 install ply==3.4 && sudo pip3 install slimit  slimit import ast slimit.parser import parser slimit.visitors import nodevisitor  data = 'a.funktion(); a.callmethod(w); e.aa(w);'  tree = parser().parse(data) node in nodevisitor.visit(tree): if isinstance(node, ast.identifier):     if node.value == 'a':         node.value = 'airplaine'     elif node.value == 'w':         node.value = 'worm'  print(tree.to_ecma()) 

it runs give output:

$ python3 src/python_renames_js_test.py airplaine.funktion(); airplaine.callmethod(worm); e.aa(worm); 

caveats:

  1. function reserved word, used funktion
  2. the to_ecma method pretty prints; there way output closer original input.

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -