Mir*_*ac7 1 python instance backslash
好吧,这听起来像是一个愚蠢的问题,但我无法解决这个问题......
我需要从下载的文件中删除所有反斜杠的实例......但是,
output.replace("\","")
Run Code Online (Sandbox Code Playgroud)
不起作用.Python认为"\","一个字符串,而不是"\"一个字符串和""另一个字符串.
我怎样才能删除反斜杠?
编辑:新问题...最初,必须处理下载的文件,我使用:
fn = "result_cache.txt"
f = open(fn)
output = []
for line in f:
if content in line:
output.append(line)
f.close()
f = open(fn, "w")
f.writelines(output)
f.close()
output=str(output)
#irrelevant stuff
with open("result_cache.txt", "wt") as out:
out.write(output.replace("\\n","\n"))
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,将文件的内容减少到只有一行...最后只有这个内容结束:
Line of text\
Another line of text\
There\\\'s more text here\
Last line of text
Run Code Online (Sandbox Code Playgroud)
我不能再使用相同的东西,因为它会将每一行转换为列表中的值,留下括号和逗号...所以,我需要:
out.write(output.replace("\\n","\n"))
out.write(output.replace("\\",""))
Run Code Online (Sandbox Code Playgroud)
在同一行......怎么样?或者还有另一种方式吗?