asimes
asimes

Reputation: 5916

Python split() not working as expected for first line in file

I have a large text file of data mined opinions and each is classified as positive, negative, neutral, or mixed. Every line begins with "+ ", "- ", "= ", or "* " which correspond to these classifiers. Additionally, lines that begin with "!! " represent a comment to ignore.

Below is a simple Python script that is just supposed to count each of the classifiers and ignore the comment lines:

classes = [0, 0, 0, 0] # "+", "-", "=", "*"

f = open("All_Classified.txt")
for i, line in enumerate(f):
    line = line.strip()
    classifier = line.split(" ")[0]

    if classifier == "+": classes[0] += 1
    elif classifier == "-": classes[1] += 1
    elif classifier == "=": classes[2] += 1
    elif classifier == "*": classes[3] += 1
    elif classifier == "!!": continue
    else: print "Line "+str(i+1)+": \""+line+"\""
f.close()

print classes

Here is a sample of the first 5 lines of "All_Classified.txt":

!! GROUP 1, 1001 - 1512
= 1001//CD TITLETITLE//NNP How//WRB many//JJ conditioners/conditioner/NNS do//VBP you//PRP have//VBP ?//. 
= 1002//CD I//PRP have//VBP two//CD different//JJ kinds/kind/NNS ,//, Garnier//NNP Fructis//NNP Triple//NNP Nutrition//NNP conditioner//NN ,//, and//CC Suave//NNP coconut//NN .//. 
= 1003//CD But//CC I//PRP think//VBP I//PRP have//VBP about//IN 8//CD bottles/bottle/NNS of//IN the//DT Suave//NNP coconut//NN My//PRP$ mom//NN gave/give/VBD me//PRP a//DT bunch//NN for//IN Christmas//NNP because//IN she//PRP was/be/VBD getting/get/VBG tired/tire/VBN of//IN me//PRP saying/say/VBG I//PRP was/be/VBD out//IN 
= 1004//CD TITLETITLE//NNP Need//VB a//DT gel//NN that//IN works/work/NNS ,//, 8//CD mo//NN ,//, post//NN ,//, ready//JJ to//TO relax//VB edges/edge/NNS ,//, HELP//NNP ,//, 

For whatever reason my output is triggering the else statement during the first iteration as if it does not recognize the "!!", I am not sure why. I am getting this as output:

Line 1: "!! GROUP 1, 1001 - 1512"
[2986, 1034, 16278, 535]

Additionally, If I delete the first line from "All_Classified.txt" then it does not recognize the "=" of what would then be the first line. Not sure what has to be done for the first line to be recognized as expected.

Edit (again): As Peter asked, here is the output if I replace else: print "Line "+str(i+1)+": \""+line+"\"" with else: print "Classifier "+classifier+ " Line "+str(i+1)+": \""+line+"\"":

Classifier !! Line 1: "!! GROUP 1, 1001 - 1512"
[2986, 1034, 16278, 535]

Edit: First section using xxd All_Classified.txt:

0000000: efbb bf21 2120 4752 4f55 5020 312c 2031  ...!! GROUP 1, 1
0000010: 3030 3120 2d20 3135 3132 0d0a 3d20 3130  001 - 1512..= 10
0000020: 3031 2f2f 4344 2054 4954 4c45 5449 544c  01//CD TITLETITL
0000030: 452f 2f4e 4e50 2048 6f77 2f2f 5752 4220  E//NNP How//WRB 

Upvotes: 1

Views: 546

Answers (1)

NPE
NPE

Reputation: 500733

I suspect your input file isn't what it seems. For example, classifier could contain some control characters that are not shown when you print it (but which affect the comparison):

>>> classifier = '!\0!'
>>> print classifier
!!
>>> classifier == '!!'
False

edit There you have it:

0000000: efbb bf21 2120
         ^^^^ ^^

It's the UTF-8 BOM, which becomes part of classifier.

Try opening the file using codecs.open() with "utf-8-sig" as the encoding (see, for example, https://stackoverflow.com/a/13156715/367273).

Upvotes: 3

Related Questions