鱼C论坛

 找回密码
 立即注册
查看: 3251|回复: 3

[已解决]python3.11求助将制定目录下的ini文件不管是什么编码都改为utf-8

[复制链接]
发表于 2023-6-26 10:15:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
要求:
中文不能为乱码
编码全部为改为utf-8
文件全部改为unix


如下我写了后有的文件的中文是乱码了,应该原来是Ascall码改为utf-8成了乱码
  1.         encoding = 'utf-8'
  2.         for root, dirs, files in os.walk(self.factoryroot_path):
  3.             for file in files:
  4.                 if file.endswith('.ini'):
  5.                     file_path = os.path.join(root, file)
  6.                     os.system('dos2unix ' + file_path)
  7.                     # 以读取二进制文件的方式打开 ini 文件
  8.                     with codecs.open(file_path, 'rb') as f:
  9.                         content = f.read().decode(encoding, 'ignore')
  10.                         
  11.                     # 以写入二进制文件的方式重新保存 ini 文件
  12.                     with codecs.open(file_path, 'wb') as f:
  13.                         f.write(content.encode(encoding))
复制代码
最佳答案
2023-6-26 10:18:00
本帖最后由 isdkz 于 2023-6-26 10:19 编辑

在你的代码中,你已经将所有的ini文件都预设为了utf-8编码来读取,这在处理那些不是utf-8编码的文件时会造成问题,如你提到的中文乱码问题。解决这个问题的一种方法是在读取文件前先判断文件的编码类型,然后使用相应的编码来读取文件。

Python标准库中没有提供检测文件编码的函数,但是我们可以使用第三方库 chardet 来判断文件的编码。下面是一个改进的代码示例:

  1. import os
  2. import codecs
  3. import chardet

  4. for root, dirs, files in os.walk(self.factoryroot_path):
  5.     for file in files:
  6.         if file.endswith('.ini'):
  7.             file_path = os.path.join(root, file)
  8.             os.system('dos2unix ' + file_path)
  9.             # 使用chardet库来判断文件的编码
  10.             rawdata = open(file_path, "rb").read()
  11.             result = chardet.detect(rawdata)
  12.             encoding = result['encoding']

  13.             # 根据检测到的编码来读取文件
  14.             with codecs.open(file_path, 'r', encoding=encoding) as f:
  15.                 content = f.read()

  16.             # 以utf-8编码重新保存 ini 文件
  17.             with codecs.open(file_path, 'w', encoding='utf-8') as f:
  18.                 f.write(content)
复制代码


注意,在写文件时我们改为了'w'模式,因为我们要以文本模式写入文件,不再需要用到'wb'模式。使用chardet库后,我们就能正确识别并处理各种编码的文件了,但是注意chardet库可能无法识别出所有类型的编码,尤其是一些非常罕见的编码。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-26 10:18:00 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-6-26 10:19 编辑

在你的代码中,你已经将所有的ini文件都预设为了utf-8编码来读取,这在处理那些不是utf-8编码的文件时会造成问题,如你提到的中文乱码问题。解决这个问题的一种方法是在读取文件前先判断文件的编码类型,然后使用相应的编码来读取文件。

Python标准库中没有提供检测文件编码的函数,但是我们可以使用第三方库 chardet 来判断文件的编码。下面是一个改进的代码示例:

  1. import os
  2. import codecs
  3. import chardet

  4. for root, dirs, files in os.walk(self.factoryroot_path):
  5.     for file in files:
  6.         if file.endswith('.ini'):
  7.             file_path = os.path.join(root, file)
  8.             os.system('dos2unix ' + file_path)
  9.             # 使用chardet库来判断文件的编码
  10.             rawdata = open(file_path, "rb").read()
  11.             result = chardet.detect(rawdata)
  12.             encoding = result['encoding']

  13.             # 根据检测到的编码来读取文件
  14.             with codecs.open(file_path, 'r', encoding=encoding) as f:
  15.                 content = f.read()

  16.             # 以utf-8编码重新保存 ini 文件
  17.             with codecs.open(file_path, 'w', encoding='utf-8') as f:
  18.                 f.write(content)
复制代码


注意,在写文件时我们改为了'w'模式,因为我们要以文本模式写入文件,不再需要用到'wb'模式。使用chardet库后,我们就能正确识别并处理各种编码的文件了,但是注意chardet库可能无法识别出所有类型的编码,尤其是一些非常罕见的编码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-26 10:31:29 | 显示全部楼层
能先帮忙看下模块安装错误的问题吗
  1. python3.11 -m pip install cchardet  
  2. Collecting cchardet
  3.   Using cached cchardet-2.1.7.tar.gz (653 kB)
  4.   Preparing metadata (setup.py) ... done
  5. Building wheels for collected packages: cchardet
  6.   Building wheel for cchardet (setup.py) ... error
  7.   error: subprocess-exited-with-error
  8.   
  9.   × python setup.py bdist_wheel did not run successfully.
  10.   │ exit code: 1
  11.   ╰─> [23 lines of output]
  12.       running bdist_wheel
  13.       running build
  14.       running build_py
  15.       creating build
  16.       creating build/lib.linux-x86_64-cpython-311
  17.       creating build/lib.linux-x86_64-cpython-311/cchardet
  18.       copying src/cchardet/version.py -> build/lib.linux-x86_64-cpython-311/cchardet
  19.       copying src/cchardet/__init__.py -> build/lib.linux-x86_64-cpython-311/cchardet
  20.       running build_ext
  21.       building 'cchardet._cchardet' extension
  22.       creating build/temp.linux-x86_64-cpython-311
  23.       creating build/temp.linux-x86_64-cpython-311/src
  24.       creating build/temp.linux-x86_64-cpython-311/src/cchardet
  25.       creating build/temp.linux-x86_64-cpython-311/src/ext
  26.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet
  27.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet/src
  28.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet/src/LangModels
  29.       gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Isrc/ext/uchardet/src -I/usr/local/python3.11/include/python3.11 -c src/cchardet/_cchardet.cpp -o build/temp.linux-x86_64-cpython-311/src/cchardet/_cchardet.o
  30.       src/cchardet/_cchardet.cpp:196:27: fatal error: longintrepr.h: No such file or directory
  31.          #include "longintrepr.h"
  32.                                  ^
  33.       compilation terminated.
  34.       error: command '/usr/bin/gcc' failed with exit code 1
  35.       [end of output]
  36.   
  37.   note: This error originates from a subprocess, and is likely not a problem with pip.
  38.   ERROR: Failed building wheel for cchardet
  39.   Running setup.py clean for cchardet
  40. Failed to build cchardet
  41. Installing collected packages: cchardet
  42.   Running setup.py install for cchardet ... error
  43.   error: subprocess-exited-with-error
  44.   
  45.   × Running setup.py install for cchardet did not run successfully.
  46.   │ exit code: 1
  47.   ╰─> [25 lines of output]
  48.       running install
  49.       /usr/local/python3.11/lib/python3.11/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  50.         warnings.warn(
  51.       running build
  52.       running build_py
  53.       creating build
  54.       creating build/lib.linux-x86_64-cpython-311
  55.       creating build/lib.linux-x86_64-cpython-311/cchardet
  56.       copying src/cchardet/version.py -> build/lib.linux-x86_64-cpython-311/cchardet
  57.       copying src/cchardet/__init__.py -> build/lib.linux-x86_64-cpython-311/cchardet
  58.       running build_ext
  59.       building 'cchardet._cchardet' extension
  60.       creating build/temp.linux-x86_64-cpython-311
  61.       creating build/temp.linux-x86_64-cpython-311/src
  62.       creating build/temp.linux-x86_64-cpython-311/src/cchardet
  63.       creating build/temp.linux-x86_64-cpython-311/src/ext
  64.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet
  65.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet/src
  66.       creating build/temp.linux-x86_64-cpython-311/src/ext/uchardet/src/LangModels
  67.       gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Isrc/ext/uchardet/src -I/usr/local/python3.11/include/python3.11 -c src/cchardet/_cchardet.cpp -o build/temp.linux-x86_64-cpython-311/src/cchardet/_cchardet.o
  68.       src/cchardet/_cchardet.cpp:196:27: fatal error: longintrepr.h: No such file or directory
  69.          #include "longintrepr.h"
  70.                                  ^
  71.       compilation terminated.
  72.       error: command '/usr/bin/gcc' failed with exit code 1
  73.       [end of output]
  74.   
  75.   note: This error originates from a subprocess, and is likely not a problem with pip.
  76. error: legacy-install-failure

  77. × Encountered error while trying to install package.
  78. ╰─> cchardet

  79. note: This is an issue with the package mentioned above, not pip.
  80. hint: See above for output from the failure.

  81. [notice] A new release of pip available: 22.3.1 -> 23.1.2
  82. [notice] To update, run: pip3 install --upgrade pip
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-26 11:13:54 | 显示全部楼层
isdkz 发表于 2023-6-26 10:18
在你的代码中,你已经将所有的ini文件都预设为了utf-8编码来读取,这在处理那些不是utf-8编码的文件时会造 ...

跪谢大大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 22:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表