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()
Die Einrückung ist unterbrochen –