我有個問題。我有一個我閱讀的檔案,我提取了和之間的所有內容\begin{acronym}(\end{acronym}這應該代表data)。然后我想在和之間再次保存我的dict( sorted_output) 。舊條目應該被洗掉。只有中間的字典應該被保存。順序很關鍵。當我執行我的代碼時,我得到了與我的某個檔案相同的檔案。\begin{acronym}\end{acronym}
代碼
# read the file
with open("./file.txt", 'r', encoding="utf-8") as file:
data = file.read().rstrip()
data = r"""\chapter*{Short}
\addcontentsline{toc}{chapter}{Short}
\markboth{Short}{Short}
\begin{acronym}[example]
\acro{knmi}[KNMI]{Koninklijk Nederlands Meteorologisch Instituut}
\acro{example}[e.g.]{For example}
\end{acronym}"""
# just transforming of the data
# ...
sorted_output = {
"abbreviation_eg": {
"symbol": "eg",
"shortform": "e.g.",
"longform": "For example"
}
"abbreviation_fbi": {
"symbol": "fbi",
"shortform": "FBI",
"longform": "Federal Bureau of Investigation"
},
"abbreviation_knmi": {
"symbol": "knmi",
"shortform": "KNMI",
"longform": "Koninklijk Nederlands Meteorologisch Instituut"
},
}
with open(abbreviationPathComplete, encoding="utf-8") as fin, open('stats.txt','w', encoding="utf-8") as fout:
for line in fin:
fout.write(line)
if(line.startswith(r'\begin{acronym}')):
next_line = next(fin)
if (next_line.startswith(r'\end{acronym}')):
for key in sorted_output:
print(sorted_output[key]["shortform"])
file.write(r'\acro{' sorted_output[key]["symbol"] '}[' sorted_output[key]["shortform"] ']{' sorted_output[key]["longform"] '}')
fout.write(next_line)
某些檔案
\chapter*{Short}
\addcontentsline{toc}{chapter}{Short}
\markboth{Short}{Short}
\begin{acronym}[example]
\acro{knmi}[KNMI]{Koninklijk Nederlands Meteorologisch Instituut}
\acro{example}[e.g.]{For example}
\end{acronym}
所需檔案
\chapter*{Short}
\addcontentsline{toc}{chapter}{Short}
\markboth{Short}{Short}
\begin{acronym}[example]
\acro{example}[e.g.]{For example}
\acro{fbi}[FBI]{Federal Bureau of Investigation}
\acro{knmi}[KNMI]{Koninklijk Nederlands Meteorologisch Instituut}
\end{acronym}
uj5u.com熱心網友回復:
根據您的評論和問題:
data = r"""\chapter*{Short}
\addcontentsline{toc}{chapter}{Short}
\markboth{Short}{Short}
\begin{acronym}[Short]
\acro{knmi}[KNMI]{Koninklijk Nederlands Meteorologisch Instituut}
\acro{example}[e.g.]{For example}
\end{acronym}"""
data = data.split('\n')
sorted_output = {
"abbreviation_eg": {
"symbol": "eg",
"shortform": "e.g.",
"longform": "For example"
},
"abbreviation_fbi": {
"symbol": "fbi",
"shortform": "FBI",
"longform": "Federal Bureau of Investigation"
},
"abbreviation_knmi": {
"symbol": "knmi",
"shortform": "KNMI",
"longform": "Koninklijk Nederlands Meteorologisch Instituut"
}
}
write = True
with open('stats.txt', 'w') as file:
for line in data:
if line.startswith(r'\begin{acronym}'):
write = False
file.write(line '\n')
for key in sorted_output:
file.write(r'\\acro{{' sorted_output[key]["symbol"] '}}[' sorted_output[key]["shortform"] ']{' sorted_output[key]["longform"] '}\n')
else:
if write:
file.write(line '\n')
else:
if line.startswith(r'\end{acronym}'):
file.write(line)
write = True
uj5u.com熱心網友回復:
我會使用這樣的東西:
replace = True
with open('stats.txt', 'w') as file:
lines = data.split('\n')
length = len(lines)
for i,line in enumerate(lines):
if (line.strip().startswith('\acro{')):
if replace:
for key in sorted_output:
print(sorted_output[key]["shortform"])
file.write('\t')
file.write(r'\acro{{' sorted_output[key]["symbol"] '}}[' sorted_output[key]["shortform"] ']{' sorted_output[key]["longform"] '}\n')
replace = False
else:
line_feed = '\n' if i<length-1 else ''
file.write(line.replace("\b", "\\b").replace("\a", "\\a") line_feed)
它只是將第一個首字母縮寫詞替換為sorted_output. 它忽略了其他首字母縮略詞行data
uj5u.com熱心網友回復:
for line in data:
在您的示例中,資料是一個字串,因此回圈在每個字母上。這從不以 r'\begin{acronym}' 開頭
所以 if 陳述句永遠不會是 True,什么都不會發生。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426907.html
