如何读取.tex文件,并使用python将其内容保存到字符串中?
我在互联网上寻找解决方案,但找不到任何有用的方法。
我使用的是Windows,而不是Linux。
我设法做到的是:
f = open("xxx.tex","a")
f.write('This is a test\n')
但是,如果我是对的,f现在是一个对象,而不是字符串。
您可以这样:
texdoc = [] # a list of string representing the latex document in python
# read the .tex file, and modify the lines
with open('test.tex') as fin:
for line in fin:
texdoc.append(line.replace('width=.5\\textwidth', 'width=.9\\textwidth'))
# write back the new document
with open('test.tex', 'w') as fout:
for i in range(len(texdoc)):
fout.write(texdoc[i])
或这样(可能会更棘手):
from __future__ import print_function
import fileinput
# inplace=True means that standard output is directed to the input file
for line in fileinput.input('test.tex', inplace=True):
print(line.replace('width=.5\\textwidth', 'width=.9\\textwidth'), end=' ')))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句