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

标签 python encoding visual-studio-code terminal character-encoding

我正在尝试传递包含韩语字符的字符串参数。这会导致错误,因为韩语字符在传递给 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 ,因为我发送到提示符的命令不知何故无法传递正确的韩语字符,现在是 ?????? 而不是它的正确名称。

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

如何将未正确传递给模块的字符串字符传递给模块?

我正在使用最新版本的 VSCode 和 Git Bash 终端。

另外,我可以检查这个信息。如果您需要更多信息,请评论。

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

最佳答案

事实证明,这不是 Python 的问题,也不是 VSCode 的问题。这只是 hwp5txt的问题在 hwp5txt.exe不会消化韩语sys.argv 。它的工作原理是尝试: $ 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."

关于python - 如何从终端传递有编码问题的字符串参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60371338/

相关文章:

python - sqlite - 如何通过整数值根据概率选择行?

python - 如何在一行中的至少一个元素中选择包含特定值的行?

python - 在 Django 1.6 中获取用户配置文件的新方法是什么?

ruby - 如何将 unicode 字符串转换为其在 Ruby 中的符号字符?

node.js - golang base64 编码 vs nodejs 缓冲区 base64 编码

python - 在 Linux 上从 Visual Studio Code 调试 Python C++ 扩展

node.js - 如何将 Intellisense 文件导入 vsCode (Visual Studio Code)

python - 递归遍历列表(python)

Flutter检查器在Visual Studio Code中错误/卡住

python - 如何将多个不同语言的 CSV 文件合并到一个 CSV 文件中?