python文件以错误的编码utf8加载

2024-09-27 07:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程相当陌生,我不理解我得到的这个错误消息,file was loaded in the wrong encoding utf-8或者它不是代码中的错误消息,但是我在我的新的.txt文件中得到了它,我在其中写入了所有找到的关键字。txt文件将upp到4000多行,其中包含我在另一个程序中对Excel进行排序,然后将其发送到Access。这个信息是什么意思?有没有办法解决它?谢谢

我用pycharm和Python36

enter image description here

import glob


def LogFile(filename, tester):

    data = []
    with open(filename) as filesearch:   # open search file
        filesearch = filesearch.readlines()   # read file
    file = filename[37:]
    for line in filesearch:
        if tester in line:   # extract "Create Time"
            short = line[30:]
            data.append(short)   # store all found wors in array

    print (file)

    with open('Msg.txt', 'a') as handler:  # create .txt file

        for i in range(len(data)):
            handler.write(f"{file}|{data[i]}")


# open with 'w' to "reset" the file.
with open('LogFile.txt', 'w') as file_handler:
    pass
# ---------------------------------------------------------------------------------

for filename in glob.glob(r'C:\Users\Documents\Access\\GTX797\*.log'):
    LogFile(filename, 'Sending Request: Tester')

Tags: intxtfordataas错误withline
1条回答
网友
1楼 · 发布于 2024-09-27 07:23:15

我只是在pyCharm中遇到了相同的错误,并在创建文件时指定了UTF-8来修复它。您需要导入编解码器才能完成此操作。在

import codecs

with codecs.open(‘name.txt', 'a', 'utf-8-sig') as f:

相关问题 更多 >

    热门问题