Forums

Beginners copy-replace Issue

Hi,

I would like to have a python3 file that can do the following:

  • take a file "blablabla9999_WD000txt"
  • replace "000" in that float text of that file with "008"
  • save file as "blablabla9999_WD008txt"
  • redo this for every file in current Directory

Note I am an absoute beginner in python and programming. I'd really appreciate if someone could help me.

Kind regards Martin

first you want to think about some considerations such as

  • if you have 00000, do you want it to replace with 00800 or 00008?

then, you can think about whether you want to write a python script to process the text files, or if you want to use some builtin commandline tools, (or maybe even search and replace from our text editor!)

Here is potentially one command to do something like what you described.

sed s/000/008/g blablabla9999_WD000txt > blablabla9999_WD008txt

Experiment with different scripts/commands (but make sure you make a backup of your data first in case you irreversibly delete anything)

man sed from the bash commandline will give you a good overview. Otherwise tr, awk etc can all fulfill similar functions. From within python, you may want to look at the regex module and string replacements.