gpt4 book ai didi

python 3 : Join lines of file if its not endswith a special character

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:22 25 4
gpt4 key购买 nike

假设一个日志文件格式如下:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com