我有以下文件
In [5]: !cat /usr/bin/locale | head -n 3
>�����P�P�P��$h%���������DDP�td<�<�<���Q�tdR�tdP�P�P���/lib64/ld-linux-x86-64.so.2GNUGNU���t�g6����SҶ��p���S
=B!4F;?"38%yG\��w'.MSec>�^��e�n�[�[ N�m
�X���v�!�WA}sx+j 1�7��fgetscallocstrlentsearchp
osix_spawn_file_actions_initstrstrstrcspn__errno_locationopen64memcmp__fxstat64stdoutfputsposix_spawn_file_actions_dest
roymemcpyfclosemallocnl_langinfoopendir__xstat64__ctype_b_locgetenvstderrmunmap_obstack_newchunkposix_spawn_file_action
s_adddup2fscanfscandirtextdomaintwalkstrchrobstack_freefprintfalphasort__stpcpyfdopenreaddir64qsortargp_parse__cxa_fina
lizeargz_create_sepposix_spawnp_obstack_beginstrcmp__libc_start_mainstrcoll__overflow__environGLIBC_2.3GLIBC_2.15GLIBC_
2.14GLIBC_2.4GLIBC_2.2.5_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTable
)u ������ii
我打算对其进行解码以供阅读
In [6]: fin = open('/usr/bin/locale', 'r')
In [8]: data = fin.read()
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-8-57f1cb056244> in <module>
----> 1 data = fin.read()
~/anaconda3/lib/python3.7/codecs.py in decode(self, input, final)
320 # decode input (taking the buffer into account)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
324 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 41: invalid continuation byte
有关文件的信息
In [10]: !file /usr/bin/locale
/usr/bin/locale: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linu
x-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=f3fee574a96736969ce794ed53d2b6cef870c7fe, stripped
In [11]: !ls -l /usr/bin/locale
-rwxr-xr-x 1 root root 54688 Aug 24 2018 /usr/bin/locale
如何将内容读取到变量中以进行进一步的操作。
答案 0 :(得分:2)
这是一个二进制可执行文件。它不包含配置。
如果要查找所有受支持的语言环境,请使用文件/usr/share/i18n/SUPPORTED
如果要查找系统的默认语言环境:(并非在所有系统上)
/etc/local.conf
要获取python
中的当前用户语言环境:
import os;
print(os.environ['LANG'])
#prints locale.encoding en_US.UTF8
print(os.environ['LANGUAGE'])
# prints en_US
要运行语言环境并将其输出转换成字符串(在Linux / Unix上最一致):
import subprocess
locale = subprocess.getoutput("/usr/bin/locale ")
您可以在上述代码中使用任何有效的参数进行语言环境设置。