[英]'str' object has no attribute 'decode' & "write() argument must be str, not bytes"
I would like to make a simple web application using Amazon Polly.我想使用 Amazon Polly 制作一个简单的 Web 应用程序。
This is my directory structure.这是我的目录结构。
.
├── data.txt
├── main.py
└── templates
└── index.html
This is my main.py这是我的 main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
from flask import send_file
from boto3 import Session
import os
import csv
from io import StringIO
from contextlib import closing
app = Flask(__name__)
@app.route("/", methods=['POST', 'GET'])
def index():
if request.method == "POST":
## Sessionを作成
session = Session(aws_access_key_id='???', aws_secret_access_key='???', region_name='us-east-1')
polly = session.client("polly")
## polly.mp3が存在した場合は削除
if os.path.isfile('/tmp/output.mp3'):
os.remove ('/tmp/output.mp3')
## data.txtをCSVとして読み込む
f = open('data.txt')
reader = csv.reader(f)
## CSVを1行ずつ読み込む
for line in reader:
## CSVの1つめの項目で男女を切り替え
if line[0] == "M":
voice="Matthew"
elif line[0] == "F":
voice="Joanna"
## 音声データを作成
response = polly.synthesize_speech(
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
OutputFormat = "mp3",
VoiceId = voice, Engine='neural', TextType='ssml')
## polly.mp3に追加書き込み
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
data = stream.read()
fw = open("/tmp/output.mp3", "a+")
fw.write(data)
fw.close()
f.close()
return send_file("/tmp/output.mp3",as_attachment=True)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run()
This is my index.html这是我的 index.html
<html>
<body>
<form action="/" method="POST">
<input type="submit" value="download">
</form>
</body>
</html>
When I tested this code locally (Windows10, Windows Subsystems for Linux, Debian, Python3.7.6), I got this error message.当我在本地(Windows10、Windows Subsystems for Linux、Debian、Python3.7.6)测试此代码时,收到此错误消息。
File "main.py", line 41, in index
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1].decode("utf-8") + '</prosody></amazon:domain></speak>',
AttributeError: 'str' object has no attribute 'decode'
So, I searched the Internet and found this page.所以,我在互联网上搜索并找到了这个页面。 'str' object has no attribute 'decode'.
'str' 对象没有属性 'decode'。 Python 3 error?
Python 3 错误?
I drop the .decode('utf-8') part:我删除 .decode('utf-8') 部分:
Text = '<speak><amazon:domain name="conversational"><prosody rate="slow">' + line[1] + '</prosody></amazon:domain></speak>',
Then I got a new error message.然后我收到了一条新的错误消息。
File "main.py", line 50, in index
fw.write(data)
TypeError: write() argument must be str, not bytes
I searched "write() argument must be str, not bytes".我搜索了“write() 参数必须是 str,而不是字节”。 A lot of websites suggest that I should put the .decode('utf-8') part.
许多网站建议我应该放置 .decode('utf-8') 部分。
Could you give me any information or suggestion?你能给我任何信息或建议吗?
As @furas commented,正如@furas 评论的那样,
open("/tmp/output.mp3", "ba+")
worked perfectly.完美地工作。 Thank you @furas!
谢谢@furas!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.