简体   繁体   English

Python无法正确读取日志文件,除非我将其内容粘贴到新的文本文件中

[英]Python can't read log file correctly unless I paste its content in a new text file

I am using open() to read a log file, but I got strange content. 我正在使用open()来读取日志文件,但是内容很奇怪。 If I open the log file by Notepad++, copy the content and paste it in new file, save it as .txt file, open() can read the correct content. 如果我通过Notepad ++打开日志文件,请复制内容并将其粘贴到新文件中,另存为.txt文件,open()可以读取正确的内容。 Code is: 代码是:

with open(a_file, 'r') as my_file:
    for line in my_file:
        line_number += 1
        if line_number == 1060 or line_number == 1061:
            print(line)

I tried many methods: 我尝试了很多方法:

  1. cat the log file and redirect to a new text file, no help 处理日志文件并重定向到新的文本文件,没有帮助
  2. Open the log file in Notepad++, save as... a new text file, no help 在记事本++中打开日志文件,另存为...一个新的文本文件,没有帮助
  3. Use linux tail command, redirect the output to a new text file, no help 使用linux tail命令,将输出重定向到新的文本文件,没有帮助
  4. Use python codecs to read it as utf-8, error happen "python codec can't decode byte 0xff in position 0: invalid start byte" 使用python编解码器将其读取为utf-8,发生错误“ python编解码器无法解码位置0的字节0xff:无效的起始字节”
  5. Open the log file in Notepad++, copy its content, paste in a new file, save to a new text file, it works. 在Notepad ++中打开日志文件,复制其内容,粘贴到新文件中,然后保存到新的文本文件中,即可正常工作。

You can't read the file because the it's encoded in UTF-16 , you can tell by the first characters which are the BOM . 您无法读取该文件,因为该文件以UTF-16编码,您可以通过第一个字符BOM来区分。 0xff is part of the BOM for UTF-16 . 0xffUTF-16的BOM的一部分。 So when reading just add encoding='utf16' (or use codecs.open with utf16 in python2) 因此,在阅读时只需添加encoding='utf16' (或在python2 codecs.openutf16一起使用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取日志文件,但在复制粘贴到记事本后可以读取 - Can't read log file but can read after copy paste to notepad python将新数据写入文件后,Logstash无法(读取和)存储文件中的日志数据 - Logstash can't (read and) store log data from a file after python write new data into it 我试图读取在我的 Python 程序中以文本形式写入的日志文件,但它返回“没有这样的文件或目录” - Im trying to read a log file which is written in text in my Python program but its returning an “No such file or directory” 似乎无法将tar.gz文件正确读取到Python中 - Can't seem to read a tar.gz file correctly into Python 在 python 中,我可以将日志写入控制台,但没有写入文件 - In python I can write a log to console but its not getting written into file 如何根据python中的特定条件将多个文本文件的内容附加到新的文本文件中? - How can I append the content of multiple text file to a new text files based on specific conditions in python? 将文件粘贴到python中的新文件中 - Paste file in new file in python 除非我在shell窗口中按Enter,否则不会使用python创建新的文本文件。 - Creating a new text file with python is not happening unless i press ENTER in shell window 当Python可以时,为什么不能仅将Content-Type:text / html添加到文本文件中? - Why can't I just add Content-Type:text/html to a text file when Python can? python读取日志并写入新文件 - python read log and write to new file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM