我有一个csv文件,其词汇格式如下:
在某些行中只有两个值(仅英语和西班牙语,没有句子)。
我读取了csv文件,如果row [3](“西班牙文句子”)为空,则在for循环中检查,否则向Amazon Polly服务器发出请求,合成mp3,存储并重命名。这可以按预期工作。
这里是我遇到问题的地方:如果row [3]有一个值,我想在row [4]中存储一个新文本,因此它应该像这样:
这是我想出的:
import csv
from botocore.exceptions import BotoCoreError, ClientError
import boto3
import sys
import os
from contextlib import closing
# from tempfile import gettempdir
import re
polly = boto3.client('polly')
folder_name = "anki-media"
# check if anki media folder extists; otherwise create it
if not os.path.exists(folder_name):
os.mkdir(folder_name)
print("Directory " , folder_name , " created ")
else:
print("Directory " , folder_name , " already exists")
with open('spanish-vocabulary.csv', encoding='utf-8') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
csv_writer = csv.writer(csv_file, delimiter=';')
for line in csv_reader:
# check if the fourth row in the csv file is empty
if line[3] in (None, ""):
pass
else:
file_name = line[3].replace(" ", "_").lower()
# csv_writer.writerow()
# line[4] = file_name
# prepare file name and remove unwanted chars
if file_name.endswith("." or "?" or "!"):
file_name = file_name[:-1]
# send request to AWS Polly
try:
spoken_text = polly.synthesize_speech(Text=line[3], OutputFormat="mp3", VoiceId="Penelope")
# store mp3 files
if "AudioStream" in spoken_text:
file = open('{}'.format(file_name + ".mp3"), 'wb')
file.write(spoken_text['AudioStream'].read())
file.close()
except (BotoCoreError, ClientError) as error:
# The service returned an error, exit gracefully
print(error)
sys.exit(-1)