0

我有以下将 boto3 用于 AWS 的代码。

import boto3
from trp import Document

# Document
s3BucketName = "bucket"
documentName = "doc.png"

# Amazon Textract client
textract = boto3.client('textract')

# Call Amazon Textract
response = textract.analyze_document(
    Document={
        'S3Object': {
            'Bucket': s3BucketName,
            'Name': documentName
        }
    },
    FeatureTypes=["FORMS"])

#print(response)

doc = Document(response)

for page in doc.pages:
    # Print fields
    print("Fields:")
    for field in page.form.fields:
        print("Key: {}, Value: {}".format(field.key, field.value))

我正在尝试将该函数的输出保存为 dict、JSON 或 CSV,但我还不是经验丰富的 python 程序员。

我试过这个:

key_map = {}
filepath = 'output.txt'
with open(filepath) as fp:
    line = fp.readline()
    cnt = 1
    while line:
        for page in doc.pages:
            # Print fields
            print("Fields:")
            for field in page.form.fields:
                #print("Key: {}, Value: {}".format(field.key, field.value))
                key_map[str(field.key, field.value)] = cnt
                line = fp.readline()
                cnt +=1

但我认为这个解决方案行不通。关于如何将 for 循环的输出保存为 JSON 的任何提示?

4

1 回答 1

0

如果要作为 csv 输出,可以将csv 模块用作:

import csv

doc = Document(response)

with open('aws_doc.csv', mode='w') as aws_field_file:
    field_write = csv.writer(aws_field_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    for page in doc.pages:
        for field in page.form.fields:
            # This will write it as your <key>, <value>
            field_write.writerow([field.key, field.value])

如果您想要文件中的标题,您还可以使用DictWriter这将使您轻松传递字典: https ://docs.python.org/3.4/library/csv.html#csv.DictWriter

于 2019-10-06T01:21:55.273 回答