如何腌制包含字符串的文件?
How do I pickle a file containing strings?
我正在尝试解析包含如下记录的日志文件:
Apr 29 06:56:48 example-server sshd[38254]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=123.183.209.132 user=root
Apr 29 06:56:50 example-server CRON[33299]: pam_unix(cron:session): session closed for user root
Apr 29 06:56:50 example-server sshd[38254]: Failed password for root from 123.183.209.132 port 55181 ssh2
Apr 29 06:56:56 example-server sshd[38254]: message repeated 2 times: [ Failed password for root from 123.183.209.132 port 55181 ssh2]
Apr 29 06:56:59 example-server sshd[38254]: fatal: Read from socket failed: Connection reset by peer [preauth]
Apr 29 06:56:59 example-server sshd[38254]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=123.183.209.132 user=root
我正在尝试 pickle 文件
with open('auth.log', 'rb') as logs:
db = pickle.load(logs)
我收到错误消息:
TypeError: a bytes-like object is required, not 'str'
您的日志文件之类的接缝不是由 pickle 模块创建的。
The pickle module implements binary protocols for serializing and
de-serializing a Python object structure. “Pickling” is the process
whereby a Python object hierarchy is converted into a byte stream, and
“unpickling” is the inverse operation [1]
因此,更好的方法可能是加载您的日志文件并将其解析为文本。例如,您可以 select 所有致命错误使用类似的东西:
db = []
with open('auth.log', 'r') as logs:
for log in logs: #for each line
if 'fatal' in log:
db.append(log)
您可以找到更多关于日志文件解析的信息here。
我正在尝试解析包含如下记录的日志文件:
Apr 29 06:56:48 example-server sshd[38254]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=123.183.209.132 user=root
Apr 29 06:56:50 example-server CRON[33299]: pam_unix(cron:session): session closed for user root
Apr 29 06:56:50 example-server sshd[38254]: Failed password for root from 123.183.209.132 port 55181 ssh2
Apr 29 06:56:56 example-server sshd[38254]: message repeated 2 times: [ Failed password for root from 123.183.209.132 port 55181 ssh2]
Apr 29 06:56:59 example-server sshd[38254]: fatal: Read from socket failed: Connection reset by peer [preauth]
Apr 29 06:56:59 example-server sshd[38254]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=123.183.209.132 user=root
我正在尝试 pickle 文件
with open('auth.log', 'rb') as logs:
db = pickle.load(logs)
我收到错误消息:
TypeError: a bytes-like object is required, not 'str'
您的日志文件之类的接缝不是由 pickle 模块创建的。
The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation [1]
因此,更好的方法可能是加载您的日志文件并将其解析为文本。例如,您可以 select 所有致命错误使用类似的东西:
db = []
with open('auth.log', 'r') as logs:
for log in logs: #for each line
if 'fatal' in log:
db.append(log)
您可以找到更多关于日志文件解析的信息here。