提问人:Snickbrack 提问时间:8/18/2023 最后编辑:Snickbrack 更新时间:8/18/2023 访问量:37
Python 正在将我的 Base64-String 解码为虚假的 String-Representation
Python is decoding my Base64-String into a false String-Representation
问:
我有以下 C# 代码,用于使用德语变音符号从字符串中创建一个 Base64-String:
var file = @"C:\Work\Podcast\Gehör\Folgen\2023-08-08 11-05-01.mkv";
var filePathBytes = Encoding.UTF8.GetBytes(file);
var encodedFilePathString = Convert.ToBase64String(filePathBytes);
然后,我将其交给 Python,以使用特定的 python 模块对这个视频文件进行一些工作,如下所示:
var startInfo = new ProcessStartInfo()
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd",
Arguments = $"/c cd {ConfigurationManager.AppSettings["PythonScriptPath"]} & {ConfigurationManager.AppSettings["PythonFilePath"]} -c \"import MyModule; MyModule.doWork('{encodedFilePathString}')\"",
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
在 Python 端,我(现在)正在尝试输出 String 以查看编码的工作原理:
decodedFileName = base64.b64decode(encodedVideoFilePath).decode('utf-8')
the_encoding = chardet.detect(base64.b64decode(encodedVideoFilePath))['encoding']
print(the_encoding, flush=True)
print(encodedVideoFilePath, flush=True)
print(decodedFileName, flush=True)
print(os.path.exists(decodedFileName), flush=True)
Python-Output 如下所示:
Received from standard out: ISO-8859-9
Received from standard out: QzpcV29ya1xQb2RjYXN0XEdlaMO2clxGb2xnZW5cMjAyMy0wOC0wOCAxMS0wNS0wMS5ta3Y=
Received from standard out: C:\Work\Podcast\Geh÷r\Folgen\2023-08-08 11-05-01.mkv
Received from standard out: True
Received from standard out:
(此输出来自我的 Process-Console,因此以“Received from standard out:”为前缀)
唯一真实的是MKV文件的字符串没有正确显示。在 Print-Output 和 python 代码的某个地方,我需要将字符串插入到其他地方,但这个 -Char 仍然存在。÷
但是即使使用这种虚假编码,Python 也可以找到文件。那么我应该怎么做才能正确处理这个 UTF-8 编码的字符串呢?
答: 暂无答案
评论
In the Print-Output and somewhere down the road in the python code where I would need the string to be inserted somewhere else but this ÷-Char is still there.
>>> print(base64.b64decode('QzpcV29ya1xQb2RjYXN0XEdlaMO2clxGb2xnZW5cMjAyMy0wOC0wOCAxMS0wNS0wMS5ta3Y=').decode('utf-8'))
,这是我在 Mac 上得到的:C:\Work\Podcast\Gehör\Folgen\2023-08-08 11-05-01.mkv
ISO-8859-9
'Gehör'.encode( 'cp1254').decode( 'cp857')
'Geh÷r'