

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# PutLexicon
<a name="PutLexiconSamplePython"></a>

以下代码示例显示如何使用基于 Python (boto3) 的应用程序将发音词典存储在 AWS 区域中。

有关此操作的更多信息，请参阅 [https://docs.aws.amazon.com/polly/latest/dg/API_PutLexicon.html](https://docs.aws.amazon.com/polly/latest/dg/API_PutLexicon.html) API 参考。

请注意以下几点：
+ 您需要通过提供本地词典文件名和存储词典名更新该代码。
+ 该示例假定您拥有在名为 `pls` 的子目录中创建的词典文件。您需要视情况新路径。

以下代码示例使用存储在 AWS 软件开发工具包配置文件中的默认凭证。有关创建配置文件的信息，请参阅 [设置AWS CLI](setup-cli.md)。

有关此操作的更多信息，请参阅 [https://docs.aws.amazon.com/polly/latest/dg/API_PutLexicon.html](https://docs.aws.amazon.com/polly/latest/dg/API_PutLexicon.html) API 参考。



```
from argparse import ArgumentParser

from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError

# Define and parse the command line arguments
cli = ArgumentParser(description="PutLexicon example")
cli.add_argument("path", type=str, metavar="FILE_PATH")
cli.add_argument("-n", "--name", type=str, required=True,
                 metavar="LEXICON_NAME", dest="name")
arguments = cli.parse_args()

# Create a client using the credentials and region defined in the adminuser
# section of the AWS credentials and configuration files
session = Session(profile_name="adminuser")
polly = session.client("polly")

# Open the PLS lexicon file for reading
try:
    with open(arguments.path, "r") as lexicon_file:
        # Read the pls file contents
        lexicon_data = lexicon_file.read()

        # Store the PLS lexicon on the service.
        # If a lexicon with that name already exists,
        # its contents will be updated
        response = polly.put_lexicon(Name=arguments.name,
                                      Content=lexicon_data)
except (IOError, BotoCoreError, ClientError) as error:
    # Could not open/read the file or the service returned an error,
    # exit gracefully
    cli.error(error)

print(u"The \"{0}\" lexicon is now available for use.".format(arguments.name))
```

