【发布时间】:2020-05-04 20:27:29
【问题描述】:
我有一个文件file.md,我想读取它并将其作为字符串获取。
然后我想获取该字符串并将其保存在另一个文件中,但作为带有引号(和所有)的字符串。原因是我想将我的降价文件的内容传输到降价字符串,以便我可以使用 javascript marked 库将其包含在 html 中。
如何使用 python 脚本做到这一点?
这是我迄今为止尝试过的:
with open('file.md', 'r') as md:
text=""
lines = md.readlines()
for line in lines:
line = "'" + line + "'" + '+'
text = text + line
with open('file.txt', 'w') as txt:
txt.write(text)
输入file.md
This is one line of markdown
This is another line of markdown
This is another one
所需输出:file.txt
"This is one line of markdown" +
"This is another line of markdown" +
(what should come here by the way to encode an empty line?)
"This is another one"
【问题讨论】:
-
您的输出文件是什么样的?它与您想要的有什么不同?
-
所以你想要的只是从文件中读取的文本前后的引号?
-
-
@MAK 基本上我想要一个字符串在我的文本文件中
-
@moctarjallo 您能否举一个所需输出的示例,就像它在目标文件中一样?
标签: python string file save read-write