ubuntu更换镜像源及巧妙使用Python脚本解决文件编码问题

一些早期项目,在windows下开发的,到linux下遇到不少编码问题。尤其是在处理大量文本文件时,乱码问题常常让人头疼不已。乱码不仅影响工作效率,还可能导致重要信息丢失。幸运的是,通过编写简单的Python脚本,我们可以轻松地解决文本文件的转码问题,大幅提升工作效率。本文将详细介绍如何使用Python脚本来批量转换文件编码,并提供一个实用的示例脚本。

ubuntu更换镜像源

bash 复制代码
cd /etc/apt/

然后把之前的镜像源复制一份存着,名字可以任意,例如first即可。

复制代码
sudo cp sources.list cources.list.fisrt

然后继续打开镜像源的相关列表:

复制代码
sudo vim sources.list

替换以下内容:

bash 复制代码
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
 

然后按 :wq 退出保存即可,最后通过 sudo apt-get upgrade 更新我们的apt即可。

分享一宝藏网站:https://fishros.com/#/fish_home

一、乱码问题概述

乱码问题通常发生在文本文件的编码不一致时。常见的编码有 GBK、UTF-8、UTF-16 等。当一个文件以一种编码方式保存,但在读取时使用了不同的编码方式时,就会出现乱码现象。

二、Python脚本实现文件转码

Python 提供了强大的文件处理功能,可以方便地实现文件的编码转换。下面是一个简单的脚本示例,用于批量转换文件夹中的所有 .txt 文件。

三、示例脚本

python 复制代码
import os

def convert_encoding(file_path, from_encoding='gbk', to_encoding='utf-8'):
    try:
        # 读取文件内容
        with open(file_path, 'r', encoding=from_encoding) as f:
            content = f.read()
        
        # 写入文件内容
        with open(file_path, 'w', encoding=to_encoding) as f:
            f.write(content)
        
        print(f"File {file_path} has been converted from {from_encoding} to {to_encoding}")
    
    except Exception as e:
        print(f"Error converting {file_path}: {e}")

def batch_convert(folder_path, from_encoding='gbk', to_encoding='utf-8'):
    if not os.path.isdir(folder_path):
        print(f"Invalid folder path: {folder_path}")
        return
    
    for root, _, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.txt'):
                file_path = os.path.join(root, file)
                convert_encoding(file_path, from_encoding, to_encoding)

# 使用示例
batch_convert('./your_folder_path')

其他资源

https://blog.csdn.net/u012689588/article/details/16950089

GL/gl.h: No such file or directory # include && cannot find -lGL - 狂奔~ - 博客园

已解决Ubuntu系统下载更新源包时提示 "Failed to fetch"错误-CSDN博客

Qt4.7 升级至 Qt5.3遇到的项目编译不过问题 - 阿青1987 - 博客园

Unity 开发技巧:一键转换脚本编码,从 GB2312 到 UTF-8_unity utf-8-CSDN博客

告别乱码噩梦!一键解决文本转码难题,工作效率翻倍!_文本乱码转码助手-CSDN博客

(转载)Windows 查找占用串口(COM)的进程 - ckrgd - 博客园

鱼香ROS社区

相关推荐
江畔何人初1 天前
pod的内部结构
linux·运维·云原生·容器·kubernetes
不做无法实现的梦~1 天前
PX4编译环境配置和qgc配置安装教程(2)
linux·stm32
齐鲁大虾1 天前
Linux下用什么编程语言方便开发B/S架构程序
linux·运维·架构
范纹杉想快点毕业1 天前
状态机设计与嵌入式系统开发完整指南从面向过程到面向对象,从理论到实践的全面解析
linux·服务器·数据库·c++·算法·mongodb·mfc
三块钱07941 天前
群晖docker部署Mattermost,对接openclaw
运维·docker·容器
博客-小覃1 天前
内网穿透/端口映射之二层穿透访问”FTP共享文件“示例操作教程
运维·服务器·网络
峥嵘life1 天前
Android16 【CTS】CtsMediaCodecTestCases等一些列Media测试存在Failed项
android·linux·学习
珠海西格1 天前
光伏电站全景感知体系:数据采集与设备状态监测技术
大数据·运维·服务器·数据库·人工智能
prettyxian1 天前
【linux】进程控制(1):从fork创建到wait回收
linux
来来走走1 天前
rocky linux 9.7安装mysql 8.4+
linux·数据库·mysql