如何从终端传递有编码问题的字符串参数?

2024-05-14 17:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试传递一个具有韩语字符的字符串参数。这会导致错误,因为在将韩语字符传递给open()内置函数之前,它显然没有正确编码/解码

我编写了一个命令,然后用os.system()执行它,这相当于在命令提示符下运行它

command = 'hwp5txt "C:\\Users\\username\\VSCodeProjects\\myproject\\data_files\\some_folder\\hwp\\2020-01-17_-_한국어가포함된 파일명(2020년도 제1차).hwp" > testdoc.txt'

os.system(command)

这会引发错误,因为韩语字符未正确解码

Traceback (most recent call last): File "C:\Users\username\AppData\Local\pypoetry\Cache\virtualenvs\asiae-bok-nlp-xpMr0EW7-py3.7\Scripts\hwp5txt-script.py", line 11, in load_entry_point('pyhwp==0.1b12', 'console_scripts', 'hwp5txt')() File "c:\users\username\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\hwp5\hwp5txt.py", line 102, in main
with closing(Hwp5File(hwp5path)) as hwp5file: File "c:\users\username\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\hwp5\filestructure.py", line 537, in init stg = Hwp5FileBase(stg) File "c:\users\username\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\hwp5\filestructure.py", line 188, in init stg = OleStorage(stg) File "c:\users\username\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\hwp5\storage\ole.py", line 35, in init self.impl = impl_class(*args, **kwargs) File "c:\users\uesrname\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\hwp5\plat\olefileio.py", line 112, in init if not isOleFile(olefile): File "c:\users\username\appdata\local\pypoetry\cache\virtualenvs\asiae-bok-nlp-xpmr0ew7-py3.7\lib\site-packages\olefile\olefile.py", line 309, in isOleFile with open(filename, 'rb') as fp: OSError: [Errno 22] Invalid argument: 'C:\Users\username\VSCodeProjects\asiae-BOK-nlp\data_files\BOK_minutes\hwp\2020-01-17_-_??????? ???(2020?? ?1?).hwp'

正如您所看到的,OS Error被引发是因为我发送到提示符的命令不知何故没有传递正确的韩语字符,现在是?????而不是它的正确名称

我在终端上手动尝试过,但也失败了

如何传递未正确传递到模块的字符串

我在Git Bash终端上使用最新版本的VSCode

另外,我可以查看这些信息。如果您需要更多信息,请发表评论

sys.stdout.encoding 
>> 'UTF-8'
sys.stdin.encoding 
>> 'cp1252'
sys.getfilesystemencoding 
>> 'UTF-8'

Tags: inpycachenlplocallineusernamevirtualenvs
1条回答
网友
1楼 · 发布于 2024-05-14 17:24:10

事实证明,这既不是Python的问题,也不是VSCode的问题。这只是{}的问题{}不会消化韩语{}。它的工作原理是: $ hwp5txt-script.py 'C:\\...\\한국어가포함된파일.hwp'

然而,有一件事让我感到困扰,那就是这个脚本可以在终端上运行,但不能在Jupyter Lab或.py脚本上运行

即, os.system(command)subprocess.run(command, shell=True)不会运行

相反,他们会弹出一个错误弹出窗口,显示:

"This file does not have an app associated with it for performing this action. Please install an app or, if one is already installed, create an association in the Default Apps Settings page."

相关问题 更多 >

    热门问题