Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

How to replace text in the multiple svg files using python

I need to alter the text in the multiple svg files please help me out from this.

 for filename in os.listdir(dirname):
      filename = dirname+filename
      #print(filename)
      #for filename in pprint(dirlist):
         if filename.endswith(".svg"):
            with open(filename) as f:
            data = f.read()
            data = data.replace('NS', 'NSSS')
            f.close()
            f = open(/files/file.svg "wt")
            f.write(data)
            f.close()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import os
import re

for filename in os.listdir(dirname):
  if filename.endswith(".svg"):
    filename = os.path.join(dirname, filename)
    with open(filename) as f:
      data = f.read()
    data = re.sub(r"NS", r"NSSS", data)
    with open('/files/file.svg', 'w') as f:
      f.write(data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...