Skip to content
Advertisement

Read out file and convert certain line into a correct form

I have a problem. I am reading in a file. This file contains abbreviations. However, I only want to read the abbreviations. This also works. However, not in the desired format as expected, I would like to save the abbreviations cleanly per line (see below for the desired output). The problem is that I’m getting something like 't\acro{.... How can I convert this to my desired output?

JavaScript
JavaScript

Desired Output

JavaScript

Advertisement

Answer

You can use re.findall() to capture all of the abbreviations, then use the json module to dump it out into a file. Your approach could work, but you’d have to do a lot of manual string parsing, which would be a pretty massive headache. (Note that a program that can parse arbitrary LaTeX would need something more powerful than regular expressions; however, since we’re parsing a very small subset of LaTeX, regular expressions will do fine here.)

JavaScript

output.json contains the following:

JavaScript
Advertisement