无法将元组转换为字符串该怎么办?



我正在尝试从元组转向字符串out_chars。但是,这似乎很麻烦,因为存在 while 循环并且状态将其定义为元组。我该怎么办

我尝试 def 转换字符串但不成功

out_chars = []        
string = ()
for i, char_token in enumerate(computer_response_generator):
out_chars.append(chars[char_token])
print(possibly_escaped_char(out_chars), end='', flush=True)
states = forward_text(net, sess, states, relevance, vocab, chars[char_token])
if i >= max_length: 
break
states = forward_text(net, sess, states, relevance, vocab, sanitize_text(vocab, "n> "))
states = convertTuple(states)
string = convertTuple(out_chars)
print(Text_to_sp(string, states))

回溯(最近一次调用(:

File "/Users/quanducduy/anaconda3/chatbot-rnn-master/chatbot.py", line 358, in <module>
main()
File "/Users/quanducduy/anaconda3/chatbot-rnn-master/chatbot.py", line 44, in main
sample_main(args)
File "/Users/quanducduy/anaconda3/chatbot-rnn-master/chatbot.py", line 92, in sample_main
args.relevance, args.temperature, args.topn, convertTuple)
File "/Users/quanducduy/anaconda3/chatbot-rnn-master/chatbot.py", line 169, in chatbot
print(Text_to_sp(string, states))
File "/Users/quanducduy/anaconda3/chatbot-rnn-master/Text_to_speech.py", line 28, in Text_to_sp
myobj.save("welcome.mp3")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gtts/tts.py", line 249, in save
self.write_to_fp(f)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gtts/tts.py", line 182, in write_to_fp
text_parts = self._tokenize(self.text)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gtts/tts.py", line 144, in _tokenize
text = text.strip()
AttributeError: 'tuple' object has no attribute 'strip'
Process finished with exit code 1

你的元组包含什么??它是否包含复杂的对象或简单的字符串数字等??? 从您上面发布的内容中很难理解您的问题。但是如果你想将元组转换为字符串,你可以这样做

new_str = ''.join(yourtuple)

我不确定是否正确理解您的问题,但是如果您想从元组制作字符串,它非常简单。

def convertTuple(tup): 
str =  ''.join(tup) 
return str
tuple = ('g', 'e', 'e', 'k', 's') 
str = convertTuple(tuple) 
print(str) 

如果无法确保元组的所有元素都是字符串,则必须强制转换它们。

''.join([str(elem) for elem in myTuple])

相关内容

最新更新