Python 记事本++;转换为UTF-8多个文件
Notepad++的函数“转换为不带BOM的UTF-8”非常好。但我有200份文件,所有的都需要保密。因此我找到了这个小python脚本:Python 记事本++;转换为UTF-8多个文件,python,utf-8,console,notepad++,Python,Utf 8,Console,Notepad++,Notepad++的函数“转换为不带BOM的UTF-8”非常好。但我有200份文件,所有的都需要保密。因此我找到了这个小python脚本: import os; import sys; filePathSrc="C:\\Temp\\UTF8" for root, dirs, files in os.walk(filePathSrc): for fn in files: if fn[-4:] != '.jar' and fn[-5:] != '.ear' and fn[-4:]
import os;
import sys;
filePathSrc="C:\\Temp\\UTF8"
for root, dirs, files in os.walk(filePathSrc):
for fn in files:
if fn[-4:] != '.jar' and fn[-5:] != '.ear' and fn[-4:] != '.gif' and fn[-4:] != '.jpg' and fn[-5:] != '.jpeg' and fn[-4:] != '.xls' and fn[-4:] != '.GIF' and fn[-4:] != '.JPG' and fn[-5:] != '.JPEG' and fn[-4:] != '.XLS' and fn[-4:] != '.PNG' and fn[-4:] != '.png' and fn[-4:] != '.cab' and fn[-4:] != '.CAB' and fn[-4:] != '.ico':
notepad.open(root + "\\" + fn)
console.write(root + "\\" + fn + "\r\n")
notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
notepad.save()
notepad.close()
它通过每个文件->我可以看到这个。但在完成后,字符集仍然是ANSI(在我的例子中):/
有人能帮我吗?我弄错了。我的记事本是德语的。因此,如果它被称为“编码”或在我的例子中是“Kodierung”和“转换为UTF-8而不带BOM”是“Konvertiere zu UTF-8 ohne BOM”,请注意
这帮了我的忙 以下是对我有效的方法: 转到记事本+++->Plugins->Plugins Admin 查找并安装Python脚本插件 使用插件->python脚本->新脚本创建新的python脚本 在脚本中插入以下代码:
import os;
import sys;
filePathSrc="C:\\Users\\YourUsername\\Desktop\\txtFolder"
for root, dirs, files in os.walk(filePathSrc):
for fn in files:
if fn[-4:] == '.txt' or fn[-4:] == '.csv':
notepad.open(root + "\\" + fn)
console.write(root + "\\" + fn + "\r\n")
notepad.runMenuCommand("Encoding", "Convert to UTF-8")
notepad.save()
notepad.close()
将C:\\Users\\YourUsername\\Desktop\\txtFolder
替换为文件所在的Windows文件夹的路径
脚本使用.txt
和.csv
文件,并忽略文件夹中的所有其他文件
使用插件->Python脚本->脚本->脚本名运行脚本是否有任何错误消息?你在“记事本++Python脚本插件”中运行了这个?也许您可以检查编码菜单中是否真的有“转换为不带BOM的UTF-8”。在我的记事本++中,只有一个“转换为UTF-8”。这可能值得更改字符串。对,我使用这个插件。在我的记事本中有“无BOM转换为UTF-8”和“转换为UTF-8”-所以两者都有。你确定第二个条件
fn[-5:]
正确吗?我认为它也应该是fn[-4:]
,因为.csv
与.txt
的长度相同。此外,如果可能的话,我建议使用endswith
方法。@turbo蝗虫对,你是我的朋友。相应地改变了。