2016-05-25 9 views
-4

Finden Sie im folgenden Programm scheint nicht zu arbeiten für '.', '!' und '?' Figuren. Kann mir jemand helfen, den Fehler zu finden?Python funktioniert nicht

Ich habe folgende Dinge ausprobiert:

ein. Setzen eines Backslash in die Suchkriterien

b. Setzen Sie zwei Backslashes in die Suchkriterien ein.

Wenn Sie sich die Ergebnisse von Print .. ansehen, werden Sie sehen, dass Fund für Sätze nicht richtig funktioniert. Kannst du mir helfen herauszufinden, was falsch ist?

Vielen Dank im Voraus!

#!/usr/bin/python 
import sys 
import csv 

# In this exercise, we are interested in the field 'body' (which is the 5th field, 
# line[4]). The objective is to count the number of forum nodes where 'body' either 
# contains none of the three punctuation marks: period ('.'), exclamation point ('!'), 
# question mark ('?'), or else 'body' contains exactly one such punctuation mark as the 
# last character. There is no need to parse the HTML inside 'body'. Also, do not pay 
# special attention to newline characters. 

def mapper(): 
    ct = 0 
    reader = csv.reader(sys.stdin, delimiter='\t') 
    writer = csv.writer(sys.stdout, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL) 

    for line in reader: 

    try: 
     if line[4].strip().find('\\.') : 
     writer.writerow(line) 
     print ".", " found" 
     ct = ct + 1 
    except: 
     print "Error from .", sys.exc_info()[0] 

    try: 
     if line[4].strip().find("!") : 
     writer.writerow(line) 
     print "!", " found" 
     ct += 1 
    except: 
     print "Error from !" 

    try: 
     if line[4].strip().find('\\?') : 
     writer.writerow(line) 
     print "?", " found" 
     ct += 1 
    except: 
     print "Error from ?"   
#   if count == 0 or count == 3 : 
#    totalLines += 1 
#    writer.writerow(line) 



test_text = """\"\"\t\"\"\t\"\"\t\"\"\t\"This is one sentence\"\t\"\" 
\"\"\t\"\"\t\"\"\t\"\"\t\"Also one sentence!\"\t\"\" 
\"\"\t\"\"\t\"\"\t\"\"\t\"Hey!\nTwo sentences!\"\t\"\" 
\"\"\t\"\"\t\"\"\t\"\"\t\"One. Two! Three?\"\t\"\" 
\"\"\t\"\"\t\"\"\t\"\"\t\"One Period. Two Sentences\"\t\"\" 
\"\"\t\"\"\t\"\"\t\"\"\t\"Three\nlines, one sentence\n\"\t\"\" 
""" 

# This function allows you to test the mapper with the provided test string 
def main(): 
    import StringIO 
    sys.stdin = StringIO.StringIO(test_text) 
    mapper() 
    sys.stdin = sys.__stdin__ 

if __name__ == "__main__": 
    main() 
+0

Die Einrückung ist unterbrochen –

Antwort

2

find(...)

S.find(sub [,start [,end]]) -> int 

Return the lowest index in S where substring sub is found, 
such that sub is contained within S[start:end]. Optional 
arguments start and end are interpreted as in slice notation. 

Return -1 on failure. 

-1 wertet als True. Wenn der Teilstring nicht gefunden wird, wird er als True ausgewertet. Wenn der Teilstring am Anfang des Strings gefunden wird, gibt er 0 zurück und wird als False ausgewertet. Wenn es an anderer Stelle in der Zeichenfolge gefunden wird, gibt es einen Index größer als Null zurück und wird auch als True ausgewertet.

Statt in verwenden:

if '.' in line[4]: 
    # ... 

Nur str.find verwenden, wenn Sie einen Index finden müssen.