gpt4 book ai didi

Python:无法将 'bytes' 对象隐式转换为 str

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:21 25 4
gpt4 key购买 nike

这是我的代码:

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)

顺便说一句,CategoryPredictor() 是一个 mrjob 项目。

每当我输入

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

在命令行中,出现错误:

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

但是 line 是一个字符串而不是一个字节对象。我做错了什么?

这是完整的回溯

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

最佳答案

您需要将字节传递给 MRJob.parse_output_line ;以二进制模式打开 input_file

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

或者在传递给方法之前对行进行编码:

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

关于Python:无法将 'bytes' 对象隐式转换为 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com