我正在尝试传递一个具有韩语字符的字符串参数。这会导致错误,因为在将韩语字符传递给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'
事实证明,这既不是Python的问题,也不是VSCode的问题。这只是{}的问题{}不会消化韩语{}。它的工作原理是:
$ hwp5txt-script.py 'C:\\...\\한국어가포함된파일.hwp'
然而,有一件事让我感到困扰,那就是这个脚本可以在终端上运行,但不能在Jupyter Lab或.py脚本上运行
即,
os.system(command)
或subprocess.run(command, shell=True)
不会运行相反,他们会弹出一个错误弹出窗口,显示:
相关问题 更多 >
编程相关推荐