假设一个日志文件格式如下:
TIMESTAMPの[INFO]のevery message ends withの
TIMESTAMPの[INFO]のa message
that contains new-line character
even its still ends withの
TIMESTAMPの[INFO]のevery message ends withの
TIMESTAMPの[ERROR]のanother message
have new-line characters in itの
TIMESTAMPの[ERROR]のevery message ends withの
我怎样才能像这样显示这个文件:
TIMESTAMPの[INFO]のevery message ends withの
TIMESTAMPの[INFO]のa message that contains new-line character even its still ends withの
TIMESTAMPの[INFO]のevery message ends withの
TIMESTAMPの[ERROR]のanother message have new-line characters in itの
TIMESTAMPの[ERROR]のevery message ends withの
最佳答案
只需过滤行,然后使用join
。
import re
with open('log_file') as file:
union = ''.join(filter(lambda line: re.match(r'\w+'), file.readlines()))
# change the regex as per your definition of 'special characters'.
关于 python 3 : Join lines of file if its not endswith a special character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48397272/