Skip to main content

Replace words in txt file

I am working in a script that open an txt file and replace any caracter that I choose for another one, so far everything is ok.
The problem is when the file that I am reading contains quote marks(") as at example attached(input.txt).
In this case, when the script faces one line that contains a simple quote mark in the middle is enough for the program launch an error message to the user (image attached). 
It's seems that IDEAScript does't deal well with this situation, when the files contains quote marks and you try to manipulate a string at any circumstances.
Having in mind that the possibility of a txt file containing quote marks is relatively high I can't just ignore this scenario.
Is there a way to overcome this obstacle using IDEAScript?
 
The script that I am using it's attached right below.

JHDCC Tue, 08/20/2019 - 06:51

In reply to by klmi

Thanks klmi, that looks like it does exactly what I'm after and is easily understandable and I can adapt it for my purposes. However, I can't work out how I can save it or run it! 
I am hoping to save this as a script, which I can call/run from another script.
Thanks for any assistance.

klmi Tue, 08/20/2019 - 08:49

In reply to by JHDCC

Hi JHDCC,
 
you don't need to run it from another script but you can ;-)
1) Copy the code in notepad and save the file with extension .py within your IDEA makro library
2) check in your IDEA options whether to run Python programs is activated (IDEA 10.3 and above necessary)
3) run the program directly from the library

JHDCC Tue, 08/20/2019 - 10:13

In reply to by klmi

Thanks again Klmi,
Nearly there I think, but I get an indentation error on line 7, which i presume is a formatting issue?
Any ideas? That's python, SQL and VB I need to do some reading on now!

klmi Tue, 08/20/2019 - 11:10

In reply to by JHDCC

Yes it's a formatting issue. The indentations in my code are destroyed (forum editor is a crap). But I'll  try to edit this. Otherwise you can make a TAB a the beginning of the lines containing f.read and f.write.

JHDCC Wed, 08/21/2019 - 05:07

In reply to by klmi

Thanks for all your help klmi. It didn't quite work as I'd hoped, so adapted the script to retain the encapsulation before removing the ".There's probably a more direct way of achieving this, but it worked.
I also had to change the server path to \\ instead of \ and add the tabs as klmi directed.
For the benefit of anyone picking this up in the future, please see attached script for reference.