Reputation:
I have a text file as shown below:
this is 1 line
this is 2 line
this is 3 line
I want to convert it into something like below:
\begin{enumerate}
\item this is 1 line
\item this is 2 line
\item this is 3 line
\end{enumerate}
I am using the code below:
prefix = '\\item '
with open('new.txt', 'r') as src:
with open('dest.txt', 'w') as dest:
dest.write("\\begin{enumerate}\n")
for line in src:
dest.write('%s%s\n' % (prefix, line.rstrip('\n')))
Reference : Python - Write To Beginning and End of Every Line in TXT
But I can't seem to add the last line "\end{enumerate}" correctly. Can someone help please?
Upvotes: 1
Views: 719
Reputation: 59184
Just add this line at the end of your with
clause:
prefix = '\\item '
with open('new.txt', 'r') as src:
with open('dest.txt', 'w') as dest:
dest.write("\\begin{enumerate}\n")
for line in src:
dest.write('%s%s\n' % (prefix, line.rstrip('\n')))
dest.write("\\end{enumerate}\n")
Upvotes: 1