user94959 user94959 - 3 months ago 10
Python Question

invalid syntax python string replace

I've been working on a little simple script in Python 3, but I'm having some trouble with the

str.replace
syntax.

this is my code:

#!/usr/bin/python3
# coding: utf-8

import os
import sys
import fileinput

for dirpath, dirnames, filenames in os.walk('.'):
for file in files:
if file.endswith('.styles'):

for dirpath in list_path_styles_files:
for line in fileinput.input(the_path, inplace=1):
# replace string for files in *.styles
line = (line.replace('font-size=15','font-size=12'))
line = (line.replace('font-size=14','font-size=12'))
line = (line.replace('font-size=18','font-size=14'))
line = (line.replace('font-size=30','font-size=18'))
line = (line.replace('font-size=36','font-size=18'))
line = (line.replace('font-size=16','font-size=12'))
line = (line.replace('font-size=26','font-size=16'))
line = (line.replace('font-size=48','font-size=20'))
line = (line.replace('font-size=28','font-size=16'))
line = (line.replace('font-size=17','font-size=14'))
line = (line.replace('font-size=21','font-size=14'))

for dirpath, dirnames, filenames in os.walk('.'):
for file in files:
if file.endswith('.layout'):

for dirpath in list_path_layout_files:
for line in fileinput.input(the_path, inplace=1):
# replace string for files in *.layout
line = (line.replace('font-size=15','font-size=12'))
line = (line.replace('font-size=14','font-size=12'))
line = (line.replace('font-size=18','font-size=14'))
line = (line.replace('font-size=30','font-size=18'))
line = (line.replace('font-size=36','font-size=18'))
line = (line.replace('font-size=16','font-size=12'))
line = (line.replace('font-size=26','font-size=16'))
line = (line.replace('font-size=48','font-size=20'))
line = (line.replace('font-size=28','font-size=16'))
line = (line.replace('font-size=17','font-size=14'))
line = (line.replace('font-size=21','font-size=14'))

print ("\n\n ## Done!\n\n")
sys.exit(0)


and this is the error I'm getting

$ ~/scripts/string_search.py
File "/home/user/scripts/string_search.py", line 16
line = (line.replace('font-size=14','font-size=12')
^
SyntaxError: invalid syntax


I'm not too sure what is wrong with this syntax and I've been trying to figure it out, but it's just not making any sense to me.

This python script is to search though specific files by walking through multiple directories finding files that ends with
*.styles
and
*.layout
to replace strings that begin with
font-size=
with it's new string values and then exit cleanly.

Give some constructive information on how to fix/improve this before you abuse that Downvote.

Answer

First of all, is the_path here

    for dirpath in list_path_layout_files:
        for line in fileinput.input(the_path, inplace=1):

is a misprint, and should be dirpath? Or from where that the_path is came from?

Second, please use True keyword instead of 1:

fileinput.input(the_path, inplace=True)

and DRY:

style_updates = ((15,12), (14,12)...)
for from_style, to_style in style_updates:
    line = line.replace('font-size={}'.format(from_style),
                        'font-size={}'.format(to_style)

Also, I suggest using re for such task.

Comments