簡體   English   中英

Python 從日志文件中讀取數據為二進制字符串而不是文本

[英]Python reads data as binary strings instead of text from log file

我有一個包含計算機日志的文件。 當我在記事本++中打開它時,前幾行看起來像這樣 -

#####################
[2015/07/03, 13:26:40.368,  INFO, 00004588] Legacy mode. Epoch number not found. Allowing message.
[2015/07/03, 13:26:40.368,  INFO, 00004588] >>>AgentGetState() called.
[2015/07/03, 13:26:40.368,  INFO, 00004588] AgentGetState Returning 00000000.<<<
[2015/07/03, 13:26:54.660,  INFO, 00010404] Subject name = am4prdapp09.fc.core.windows.net
[2015/07/03, 13:26:54.660,  INFO, 00010404] Legacy mode. Epoch number not found. Allowing message.
######################

現在,我像這樣在 python 中閱讀它們 -

f = open("log_file.log")
a = []
for i in f:
    a.append(i)

當我現在看內容時:

a[0]

這是我看到的——

'\xff\xfe[\x002\x000\x001\x005\x00/\x000\x007\x00/\x000\x003\x00,\x00 \x001\x003\x00:\x002\x006\x00:\x004\x000\x00.\x003\x006\x008\x00,\x00 \x00 \x00I\x00N\x00F\x00O\x00,\x00 \x000\x000\x000\x000\x004\x005\x008\x008\x00]\x00 \x00
\x00j\x00e\x00c\x00t\x00 \x00n\x00a\x00m\x00e\x00 \x00=\x00 \x00a\x00m\x004\x00p\x00r\x00d\x00a\x00p\x00p\x000\x009\x00.\x00f\x00c\x00.\x00c\x00o\x00r\x00e\x00.\x00w\x00i\x00n\x00d\x00o\x00w\x00s\x00.\x00n\x00e\x00t\x00\r\x00\n'

我錯過了什么?

嘗試:

f = open("log_file.log", 'r')
data = f.readlines()
for i in data:
    print i
>>> line
>>> ...
>>> ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM