

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 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 SDK 組態檔案中的預設登入資料。如需建立組態檔的資訊，請參閱「[設定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))
```

