解析文本字符串中的元素(在列表中)

时间:2018-08-09 11:35:29

标签: python

我有一个列表元素,它是文本。

print ((temp_list))

输出:

['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']

我希望得到以下输出:

Aug 7 14:23

我试图删除空格,但这会使输出混乱,这使得分离想要的元素变得更加困难。

5 个答案:

答案 0 :(得分:2)

您可以分割文本并获取第5,第6和第9字段:

f = temp_list[0].split()
print(' '.join((f[4], f[5], f[8])))

答案 1 :(得分:1)

使用正则表达式。

import re
temp_list = ['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']

for i in temp_list:
    m = re.search(r"(?P<date>(Jun|Jul|Aug|Sep).*?)\(", i)
    if m:
        print(m.group('date'))

输出:

Aug  7 14:22 - 14:23 

答案 2 :(得分:1)

sample = 'root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    '

# split the string on space characters
data = sample.split(' ')

# inspect our list in console, the list should now contain mix of words and spaces (empty string)
print(data)

# since empty string evaluates to False in Python, we can remove them like this from our list with filter function
data = filter(lambda x: x, data)


# outputs: ['root', 'pts/3', '100.121.17.73', 'Tue', 'Aug', '7', '14:22', '-', '14:23', '(00:00)']
print(data)

# in the end we collect relevant data by slicing the list
# from index 3rd to 6th and join them into one string with that data separated by one space in-between.
result = ' '.join(data[3:6])

# outputs: Tue Aug 7
print(result)

答案 3 :(得分:0)

如果您的字符串中总是带有'Tue Aug 7 14:22-14:23'模式,那么我建议您使用regex来匹配此模式:

for (i = 0; i < pageSearchInput.length; i++) {

    pageSearchInput[i].addEventListener("focus", function(){

        pageSearchContainer[i].style.outline = "4px solid #ffcc33";
        pageSearchButton[i].style.backgroundColor = "#008920";
        pageSearchButton[i].style.border = "1px solid #008920";

    });

}

答案 4 :(得分:0)

或者:

l=['root     pts/3        100.121.17.73    Tue Aug  7 14:22 - 14:23  (00:00)    ']
print(' '.join(l[0].split()[-6:][:-1]))

输出:

Aug  7 14:22 - 14:23