【发布时间】:2021-11-22 15:25:33
【问题描述】:
我正在处理这个项目,但需要帮助。我的主要目标是使翻译后的文本文件看起来与原始文件相同,但翻译后的单词除外。
原始文件的 sn-p 如下所示:
这是我的python代码:
# Step 1: Import the english.txt file
import json
english_text = open('/home/jovyan/english_to_lolspeak_fellow/english.txt', 'r')
text = english_text.readlines()
english_text.close()
# Step 2: Import the glossary (the tranzlashun.json file)
with open('/home/jovyan/english_to_lolspeak_fellow/tranzlashun.json') as translationFile:
data = json.load(translationFile)
# Step 3:Translate the English text into Lolspeak
translated_text= ''
for line in text:
for word in line.split():
if word in data:
translated_text += data[word.lower()]+" "
else:
translated_text += word.lower()+ " "
pass
# Step 4 :Save the translated text as the "lolcat.txt" file
with open('/home/jovyan/english_to_lolspeak_fellow/lolcat.txt', 'w') as lolcat_file:
lolcat_file.write(translated_text)
lolcat_file.close()
最后,我的输出如下所示:
如您所见,我能够翻译文件,但忽略了原始间距。如何更改我的代码以保持原来的间距?
【问题讨论】:
标签: python for-loop readlines writefile