N
N
NOONE2015-10-24 19:51:05
Python
NOONE, 2015-10-24 19:51:05

Python log parser outputting empty strings to list by regular value? why?

Hello everyone, I wrote an elementary parser, I need to find in the file 8853738406815367648 these values, consisting of 19 characters, only numbers, the regular expression is simple, but for some reason it also displays empty values ​​of the form [] how to get rid of them? and another question, how to remove duplicate values ​​in the list that get there after I parsed the log, thanks for the answers.
Python version 2.7

from datetime import datetime
import re
from subprocess import Popen, PIPE
import subprocess
import shutil
Message = []
stroka = []
index= []

for line in open('X:\\fdf\\lol.txt', 'r').readlines():
    index = re.findall('\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d' , line)
    #print index
    stroka.append(index)
    
filter(lambda i: i != '', stroka)
print stroka

Here is an example output of [], [], [], [], [], [], ['6306436101153673729'], ['1796074986052821275'], [], ['1796074986052821275'],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Dugin, 2015-12-30
@Djam36

If the file is not too big, you can read it in its entirety:

with open(r'z:\test.txt') as f:
    print set(re.findall('\d{19}', f.read()))

Otherwise, line by line:
with open(r'z:\test.txt') as f:
    print {item for line in f for item in re.findall('\d{19}', line)}

R
Roman Kitaev, 2015-10-24
@deliro

1) Horror, not a regular season. \d{19}isn't it easier?
2) Set (set) cannot contain duplicates.
431b05b9b65a4cbfbb3d53a86b42763d.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question