如何在 python 中获得有效的反斜杠

How do I get a working backslash in python

我有一个编码字符串作为字符串保存在一个文件中,可以更改它,如果可以的话。我想阅读它并取回真正的字符串。抱歉我不太会解释xD,这是我的代码:

def saveFile(src, con):
    with open(src, "w") as f:
        f.write(str(con))
        f.close()

...
string = "юра"
saveFile("info", mlistsaver.encode())

这是 'info' 文件:` b'\xd1\x8e\xd1\x80\xd0\xb0' 但是当我使用这个时:

def get(src):
    f = src
    if path.isfile(f):
        with open(f, "r") as f:
            return f.read()
    else:
        return None

...


get("info").encode('iso-8859-1').decode('utf-8')

字符串只是:b'\xd1\x8e\xd1\x80\xd0\xb0' 我知道它必须用 double \ 做一些事情,但我无法修复它。正如已经说过的,我可以将字符串保存为您想要的任何格式,我认为我这样做的方式真的很愚蠢。

谢谢大家!

非常感谢 BpY 和 lenz,现在可以使用了,这是我的代码:

def saveFile(src, con):
    with open(src, "wb") as f:
        f.write(con)
        f.close()

def get(src):
    f = src
    if path.isfile(f):
        with open(f, "rb") as f:
            return f.read()
    else:
        return None

...

info="юра"
saveFile("info", info.encode())
print(get("info").decode("utf-8"))

输出为:юра

再次感谢您,祝您有愉快的一天:)