2017-05-07 5 views
0

Ich versuche, die Ausgabe einer Regex-Suche zu zählen, die ich an einem Dataset durchführe, aber aus irgendeinem Grund ist meine Zählung um einiges geringer. Ich habe mich gefragt, was ich falsch mache und wie ich eine offizielle Zählung bekommen kann. Ich sollte ungefähr 1500 Übereinstimmungen haben, aber ich bekomme immer einen Fehler, der sagt "'int' Objekt ist nicht iterierbar".Regex Output Count

import re 

with open ('Question 1 Logfile.txt' , 'r') as h: 
    results = [] 
    count = [] 
    for line in h.readlines(): 
     m = re.search(r'(((May|Apr)(\s*)\w+\s\w{2}:\w{2}:\w{2}))', line) 
     t = re.search(r'(((invalid)(\s(user)\s\w+)))',line) 
     i = re.search(r'(((from)(\s\w+.\w+.\w+.\w+)))', line) 
     if m and t and i: 
      count += 1 
      print(m.group(1),' - ',i.group(4),' , ',t.group(4)) 
      print(count) 
+0

hier zählen ist eine Liste während der Initialisierung und Sie können ein Listenobjekt nicht erhöhen.Zählen Sie zählen = 0 – bigbounty

+0

@bigbounty Ja, danke Ich kann nicht glauben, dass ich das verpasst habe. Eine letzte kurze Frage, diese Methode zählt jede Ausgabe wie sie kommt, wie kann ich am Ende eine Gesamtzählung machen? – user7823345

+0

@bigbounty Wie kann ich einen Kommentar upvote? und kannst du erklären, was du meinst, indem du count.append (1) sagst? – user7823345

Antwort

0

Sie möchten die Anzahl erhöhen, mit der Sie eine Bedingung über eine Reihe von Schleifeniterationen erfüllen. Die Verwirrung scheint hier zu sein, wie genau das zu tun ist und welche Variable erhöht werden soll.

Hier ist ein kleines Beispiel, das die aufgetretene Schwierigkeit beschreibt, wie in OP- und OP-Kommentaren beschrieben. Es ist als Lernbeispiel gedacht, bietet aber auch einige Optionen für eine Lösung.

count = [] 
count_int = 0 

for _ in range(2): 
    try: 
     count += 1 
    except TypeError as e: 
     print("Here's the problem with trying to increment a list with an integer") 
     print(str(e)) 

    print("We can, however, increment a list with additional lists:") 
    count += [1] 
    print("Count list: {}\n".format(count)) 

    print("Most common solution: increment int count by 1 per loop iteration:") 
    count_int +=1 
    print("count_int: {}\n\n".format(count_int)) 

print("It's also possible to check the length of a list you incremented by one element per loop iteration:") 
print(len(count)) 

Ausgang:

""" 
Here's the problem with trying to increment a list with an integer: 
'int' object is not iterable 

We can, however, increment a list with additional lists: 
Count list: [1] 

Most common is to increment an integer count by 1, for each loop iteration: 
count_int: 1 


Here's the problem with trying to increment a list with an integer: 
'int' object is not iterable 

We can, however, increment a list with additional lists: 
Count list: [1, 1] 

Most common is to increment an integer count by 1, for each loop iteration: 
count_int: 2 


It's also possible to check the length of a list you incremented 
by one element per loop iteration: 
2 
""" 

Hoffnung, das hilft. Viel Glück, Python zu lernen!