0

I'm trying to modify a specific line in a js file using python.

Here's the js file :

...
hide: [""]
...

Here's my python code :

with open('./config.js','r') as f:
   lines = f.readlines()

with open('./config.js','w') as f:
   for line in lines:
       line = line.replace('hide', 'something')
       f.write(line)

So it works but this is not what I want to do.

I want to write 'something' between the brackets and not replace 'hide'.

So I don't know how to do it: Do I have to replace the whole line or can I just add a word between the brackets?

Thanks

William
  • 27
  • 1
  • 2
  • 8

5 Answers5

4

If you want to replace text at this exact line you could just do:

with open('./config.js','r') as f:
   lines = f.readlines()

with open('./config.js','w') as f:
   new_value = 'Something New'
   for line in lines:
       if line.startswith('hide'):
           line = 'hide: ["{}"]'.format(new_value)
       f.write(line)

or alternatively in the conditional

           if line.startswith('hide'):
               line = line.replace('""', '"Something new"')

Here's way to replace any value in brackets for hide that starts with any spacing.

lines = '''\
first line
            hide: [""]
       hide: ["something"]
last line\
'''
new_value = 'new value'

for line in lines.splitlines():
    if line.strip().startswith('hide'):
       line = line[:line.index('[')+2] + new_value + line[line.index(']')-1:]
    print(line)

Output:

first line
            hide: ["new value"]
       hide: ["new value"]
last line
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
1

If hide: [""] is not ambiguous, you could simply load the whole file, replace and write it back:

newline = 'Something new'

with open('./config.js','r') as f:
   txt = f.read()

txt = txt.replace('hide: [""]', 'hide: ["' + newline + '"]')

with open('./config.js','w') as f:
   f.write(txt)
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
1

You can use fileinput and replace it inplace:

import fileinput
import sys

def replaceAll(file,searchExp,replaceExp):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp)
        sys.stdout.write(line)

replaceAll("config.js",'hide: [""]','hide: ["something"]')

Reference

dalonlobo
  • 484
  • 4
  • 18
0

You can do this using re.sub()

import re
with open('./config.js','r') as f:
   lines = f.readlines()

with open('./config.js','w') as f:
   for line in lines:
       line = re.sub(r'(\[")("\])', r'\1' + 'something' + r'\2', line)
       f.write(line)

It works by searching for a regular expression, but forms a group out of what you want on the left ((\[")) and the right (("\])). You then concatenate these either side of the text you want to insert (in this example 'something').

The bounding ( ) makes a group which can be accessed in the replace with r'\1', then second group is r'\2'.

slackline
  • 2,295
  • 4
  • 28
  • 43
0

As long as you don't have "hide" anywhere else in the file, then you could just do

with open('/config.js','r') as f:
    lines = f.readlines()

with open('./config.js','w') as f:
    for line in lines:
        line = line.replace('hide [""]', 'hide ["something"]')
        f.write(line)
Delkarix
  • 84
  • 11