如何删除文件中每一行的一部分?

时间:2018-07-15 20:19:00

标签: python python-2.7

我正在尝试打印一些日志文件,但是我想消除日志文件中每一行的第一部分。 例如:

[2018-07-10 15:04:11] USER INPUT "hello"
[2018-07-10 15:04:12] SYSTEM RESPONSE: "Hello! How are you doing today"
[2018-07-10 15:04:42] USER INPUT "I am doing good thank you"
[2018-07-10 15:04:42] SYSTEM RESPONSE: "Good to know"

我只想要

USER INPUT "hello"
SYSTEM RESPONSE: "Hello! How are you doing today"
USER INPUT "I am doing good thank you"
SYSTEM RESPONSE: "Good to know"

当前代码:

import os
location = '/Users/user 1/Desktop/'

f = open(os.path.join(location, 'xvp.log'), "r")

print(f.read())

3 个答案:

答案 0 :(得分:2)

这是一个开始

import os
location = '/Users/user 1/Desktop/'

f = open(os.path.join(location, 'xvp.log'), "w+")

for line in f.readlines():
    index_ = line.index(']') + 2
    new_line = line[index_:]
    # TODO: save the new_line to the file

f.close()

答案 1 :(得分:0)

您可以尝试re模块:

s = '''[2018-07-10 15:04:11] USER INPUT "hello"
[2018-07-10 15:04:12] SYSTEM RESPONSE: "Hello! How are you doing today"
[2018-07-10 15:04:42] USER INPUT "I am doing good thank you"
[2018-07-10 15:04:42] SYSTEM RESPONSE: "Good to know"'''

import re

print(re.sub(r'\[(.*?)\]\s+', '', s))

打印:

USER INPUT "hello"
SYSTEM RESPONSE: "Hello! How are you doing today"
USER INPUT "I am doing good thank you"
SYSTEM RESPONSE: "Good to know"

要将其连接到代码,只需从文件到变量读取字符串,然后使用re.sub函数。

答案 2 :(得分:0)

我的正则表达式不太好,因此欢迎输入。您可以使用正则表达式解决此问题-

^[[]\d{4}[-]\d{2}[-]\d{2}[ ]\d{2}[:]\d{2}[:]\d{2}[]][ ]

我为什么使用^?这样它就从您的字符串的开头开始匹配,而不匹配字符串中间的[,然后匹配整个模式。现在您可以使用python的re模块,如-

import re
catcher = u'^[[]\d{4}[-]\d{2}[-]\d{2}[ ]\d{2}[:]\d{2}[:]\d{2}[]][ ]'
your_string = '[2018-07-10 15:04:12] SYSTEM RESPONSE: "Hello! How are you doing today"'
your_string = re.sub(catcher, '', your_string)
# re.sub will replace all the matches
# It takes - (regex_pattern, replace_the_matches_with, your_match_string) 

输出-SYSTEM RESPONSE: "Hello! How are you doing today"