我正在处理下面这样的字符串:
my_string = '{"amountexvat":"0.14","amountincvat":"0.15","currency":"EUR","paymentmethods":[{"details":{"acquirerapprovalcode":"037464","acquirerid":"A0000000031010","acquirerpoiid":"gyytfft55","adyentransactionid":"1234","adyentransactiontimestamp":"2021-10-08T15:28:07.000Z","cardname":"kll","cardtype":"hhgh","entrymodes":["ICC"],"flourpaymentreference":"889999","merchantid":"888","paymentbrand":"test","paymentvariant":"visastandardcredit","poitransactionid":"12345","poitransactionreconciliationid":"1000","poitransactiontimestamp":"2021-10-08T17:28:07.000Z","terminalid":"866777","transactionamount":0.15,"transactioncurrency":"EUR"},"type":"ec"}],"products":[{"amount":1,"baseprice":"0.15","discount":"0","discountedprice":null,"itemvat":"0.01","pricepaidexvat":"0.14","pricepaidincvat":"0.15","unit":"piece","vendorchangedprice":true}],"salevat":"0.01"}'
另一方面,我有 FPDF 对象(生成一些报告)。我想将 json 视图上传my_string
到 FPDF 对象。
import fpdf
pdf = FPDF()
#Cover page
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(175, 10, 'TEST - report',0,1,'C')
你知道怎么处理吗?
my_string
我的意思是Json视图:
我在 EMR AWS 上使用 jupyter notebook,我尝试将 json 文件(基于my_string
)保存到 S3 存储桶,稍后以读取模式打开 json 并将该文件中的文本插入 pdf。
# save json to file in bucket
s3 = boto3.client('s3')
json_object = my_string
s3.put_object(
Body=json.dumps(json_object),
Bucket='my_bucket',
Key='my_key'
)
# open the json file in read mode
s3client = boto3.client('s3')
# create a file object using the bucket and object key
fileobj = s3client.get_object(
Bucket='my_bucket',
Key='my_key'
)
# open the file object and read it into the variable filedata
filedata = fileobj['Body'].read()
# decode file
contents = filedata.decode('latin-1')
# insert the texts in pdf
for x in contents:
pdf.cell(200, 10, txt = x, ln = 1, align = 'C')
不幸的是,它没有按我的意愿工作(文本保存在 pdf 报告中,但不是 json 格式)。你知道如何正确地将 json 的内容写入 PFDF 对象吗?
感谢您的任何想法。