将打印输出保存为 dict 或 JSON

我有以下将 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 的任何提示?


缥缈止盈
浏览 125回答 1
1回答

芜湖不芜

如果要作为 csv 输出,可以将csv 模块用作:import csvdoc = Document(response)with open('aws_doc.csv', mode='w') as aws_field_file:&nbsp; &nbsp; field_write = csv.writer(aws_field_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)&nbsp; &nbsp; for page in doc.pages:&nbsp; &nbsp; &nbsp; &nbsp; for field in page.form.fields:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # This will write it as your <key>, <value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field_write.writerow([field.key, field.value])如果您想要文件中的标题,您还可以使用DictWriter这将使您轻松传递字典: https ://docs.python.org/3.4/library/csv.html#csv.DictWriter
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python