[英]Changing encoding of *.txt files withing a directory with Python
I would like to change encoding of .txt files in a directory with python to UTF-8, are there ways to do that ?我想将 python 目录中的 .txt 文件的编码更改为 UTF-8,有没有办法做到这一点?
Thank you for your support.感谢您的支持。
I have viewed the solution already mentionned by stackoverflow users here : How to convert a file to utf-8 in Python?我在这里查看了 stackoverflow 用户已经提到的解决方案: How to convert a file to utf-8 in Python?
I would like to apply it for all files of particular category in the directory and not one file.我想将它应用于目录中特定类别的所有文件,而不是一个文件。
import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile:
with codecs.open(targetFileName, "w", "utf-8") as targetFile:
while True:
contents = sourceFile.read(BLOCKSIZE)
if not contents:
break
targetFile.write(contents)
1) I would like to change encoding of files in a directory to UTF-8, I know the input encoding. 1) 我想将目录中文件的编码更改为 UTF-8,我知道输入编码。
2) are there solutions to transform to UTF-8 without knowing the input encoding ? 2) 是否有在不知道输入编码的情况下转换为 UTF-8 的解决方案? ( not important at this time, but if a solution already exist, it will be great to know about it)
(此时不重要,但如果已经存在解决方案,了解它会很棒)
Put the below line above with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile
line of code:将下面的行
with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile
代码行:
for sourceFileName in os.listdir("./Your_File_path"):
If you want to do only .txt files and in your path their are other files also..do it by glob
如果你只想做 .txt 文件并且在你的路径中它们还有其他文件..通过
glob
来做
import glob
for filename in glob.glob('*.txt'):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.