2017-05-16 9 views
1

Voici mon code:Python: Impossible de convertir 'octets' objet str implicitement

class ReviewCategoryClassifier(object): 
     @classmethod 
     def load_data(cls, input_file): 
      job = category_predictor.CategoryPredictor() 
      category_counts = None 
      word_counts = {} 

      with open(input_file) as src: 
       for line in src: 
       category, counts = job.parse_output_line(line) 

     def __init__(self, input_file): 
     """input_file: the output of the CategoryPredictor job.""" 
      category_counts, word_counts = self.load_data(input_file) 

      self.word_given_cat_prob = {} 
      for cat, counts in word_counts.iteritems(): 
       self.word_given_cat_prob[cat] = self.normalize_counts(counts) 

       # filter out categories which have no words 
       seen_categories = set(word_counts) 
       seen_category_counts = dict((cat, count) for cat, count in 
             category_counts.iteritems() \ 
              if cat in seen_categories) 
       self.category_prob= self.normalize_counts(
                 seen_category_counts) 

if __name__ == "__main__": 
    input_file = sys.argv[1] 
    text = sys.argv[2] 
    guesses = ReviewCategoryClassifier(input_file).classify(text) 

BTW CategoryPredictor() est un projet mrjob.

Chaque fois que je tapais dans

python predict.py yelp_academic_dataset_review.json 'I like donut'

dans la ligne de commande, il a une erreur qui dit:

TypeError: Can't convert 'bytes' object to str implicitly

Mais la ligne est une chaîne plutôt un objet octets. Qu'ai-je fait de mal?

Voici le retraçage complet

Traceback (most recent call last): 
File "predict.py", line 116, in <module> 
    guesses = ReviewCategoryClassifier(input_file).classify(text) 
File "predict.py", line 65, in __init__ 
    category_counts, word_counts = self.load_data(input_file) 
File "predict.py", line 44, in load_data 
    category, counts = job.parse_output_line(line) 
File "//anaconda/lib/python3.5/site-packages/mrjob/job.py", line 961, in 
    parse_output_line 
return self.output_protocol().read(line) 
File "//anaconda/lib/python3.5/site-packages/mrjob/protocol.py", line 84, in 
    read 
     raw_key, raw_value = line.split(b'\t', 1) 
TypeError: Can't convert 'bytes' object to str implicitly 
+0

Le 'b '\ t'' est un octets au lieu d'un' str'. Essayez d'utiliser 'with open (input_file, 'r', encoding =" utf-8 ") comme src'. – stamaimer

Répondre

1

Vous devez passer à octets MRJob.parse_output_line; ouvrir input_file en mode binaire

with open(input_file, 'rb') as src: 
    for line in src: 
     category, counts = job.parse_output_line(line) 

ou coder la ligne avant de passer à la méthode:

with open(input_file) as src: 
    for line in src: 
     category, counts = job.parse_output_line(line.encode()) 
+0

Salut: si j'ai ajouté 'rb', l'erreur est devenue: raw_key, raw_value = line.split (b '\ t', 1) 0Euroption: pas assez de valeurs à décompresser (2 attendues, 1) –

+0

@CandiceZhang, Is la sortie MRJob 'input_file'? – falsetru

+0

Salut: Je pense à la même chose maintenant. Le fichier d'entrée est le premier argument de ma ligne de commande, qui est un fichier de données, au lieu de sortir de MRjob. Comment puis-je transmettre une sortie MRjob dans la ligne de commande? Merci! –