Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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()))
with open(r'z:\test.txt') as f:
print {item for line in f for item in re.findall('\d{19}', line)}
1) Horror, not a regular season. \d{19}
isn't it easier?
2) Set (set) cannot contain duplicates.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question