Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将函数指定给字典值时出错_String_Python 3.x_Function_Dictionary_Text - Fatal编程技术网

String 将函数指定给字典值时出错

String 将函数指定给字典值时出错,string,python-3.x,function,dictionary,text,String,Python 3.x,Function,Dictionary,Text,我正在尝试使用以下命令为dict的值分配函数: x_text = [clean_str(v) for k, v in answer.items()] 功能清洁_str: def clean_str(string): # remove stopwords # string = ' '.join([word for word in string.split() if word not in cachedStopWords]) string = re.sub(r"[^A-Za

我正在尝试使用以下命令为dict的值分配函数:

x_text = [clean_str(v) for k, v in answer.items()]
功能清洁_str:

def clean_str(string):
    # remove stopwords
    # string = ' '.join([word for word in string.split() if word not in cachedStopWords])
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip().lower()
但我得到了以下错误:

文件“C:\ProgramData\Anaconda3\lib\re.py”,第191行,子文件 return\u compile(模式、标志).sub(repl、字符串、计数)

TypeError:应为字符串或类似字节的对象

我的dict(答案{})的前2个k,v对的片段如下:

In[45]:{k: answer[k] for k in list(answer)[:2]}
Out[45]: 
{b'B00308CJ12': [b'Bulletproof Salesman (2008)'],
 b'189138922X': [b'Classical Mechanics']} 

dict的值都是字节而不是字符串,并且
re.sub
只能处理字符串

您应该使用
decode()
方法将字节转换为字符串:

x_text = [clean_str(i.decode()) for k, v in answer.items() for i in v]

请显示
clean_str()
的代码。编辑的原始qn…>>错误:'list'对象没有属性'decode'。如果您的值只有一个项,则不确定为什么它是一个列表,但您可以始终在列表理解中使用另一个for循环来提取其中的值。我已经相应地更新了我的答案。