ubuntu下txt文件编码转换、查询(gbk/ISO-8859->utf-8)

本文介绍在Ubuntu系统中运行Python程序时遇到的编码错误,详细解释如何使用iconv命令将文件从ISO-8859编码转换为UTF-8编码,确保程序中的汉字能正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在ubuntu下运行python程序,总是报错,说编码不对不是utf-8(貌似是因为汉字部分出问题了),搜集资料后完成了转换。

1.文件编码查询

$ file 199801.txt
199801.txt: ISO-8859 text, with very long lines, with CRLF, LF line terminators

2.完成转换

$ sudo iconv -f 'gbk' -t 'utf-8' 199801.txt > 199801_utf8.txt

3.不放心的可以再查一下

$ file 199801_utf8.txt
199801_utf8.txt: UTF-8 Unicode text, with very long lines, with CRLF, LF line terminators

此时编码转换已经完成,可以看到汉字部分已经正常了,o**k

root@ubuntu:/home/sunrise/Desktop/ydlidar_ws# ros2 launch ydlidar_ros2_driver qc.py [INFO] [launch]: All log files can be found below /root/.ros/log/2025-07-22-18-34-39-945635-ubuntu-46942 [INFO] [launch]: Default logging verbosity is set to INFO Task exception was never retrieved future: <Task finished name='Task-2' coro=<LaunchService._process_one_event() done, defined at /opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py:226> exception=InvalidLaunchFileError('py')> Traceback (most recent call last): File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_file_utilities.py", line 53, in get_launch_description_from_any_launch_file return loader(launch_file_path) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/python_launch_file_utilities.py", line 62, in get_launch_description_from_python_launch_file launch_file_module = load_python_launch_file_as_module(python_launch_file_path) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/python_launch_file_utilities.py", line 37, in load_python_launch_file_as_module loader.exec_module(mod) File "<frozen importlib._bootstrap_external>", line 844, in exec_module File "<frozen importlib._bootstrap_external>", line 981, in get_code File "<frozen importlib._bootstrap_external>", line 911, in source_to_code File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/sunrise/Desktop/ydlidar_ws/install/ydlidar_ros2_driver/share/ydlidar_ros2_driver/launch/qc.py", line 19 SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xbc in position 0: invalid start byte The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 228, in _process_one_event await self.__process_event(next_event) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 248, in __process_event visit_all_entities_and_collect_futures(entity, self.__context)) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 38, in visit_all_entities_and_collect_futures sub_entities = entity.visit(context) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/action.py", line 108, in visit return self.execute(context) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/actions/include_launch_description.py", line 130, in execute launch_description = self.__launch_description_source.get_launch_description(context) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_source.py", line 84, in get_launch_description self._get_launch_description(self.__expanded_location) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_description_source.py", line 53, in _get_launch_description return get_launch_description_from_any_launch_file(location) File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_file_utilities.py", line 56, in get_launch_description_from_any_launch_file raise InvalidLaunchFileError(extension, likely_errors=exceptions) launch.invalid_launch_file_error.InvalidLaunchFileError: Caught exception when trying to load file of format [py]: (unicode error) 'utf-8' codec can't decode byte 0xbc in position 0: invalid start byte (qc.py, line 19)
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值