通过AWS GLUE将文件转换XML到S3中的JSON

发布于 2025-02-08 17:52:28 字数 823 浏览 2 评论 0原文

我的存储桶结构如下,并且在此S3存储夹文件夹中有XML文件降落。

S3:/Fin-app-ops/data-ops/raw-d

需要将这些XML文件转换为JSON文件,并在同一存储桶中放回S3,但是不同的文件夹: S3:/Fin-app-ops/data-ops/con-D

我尝试过,但没有起作用:

import os
import json
import boto3
import xmltodict

s3 = boto3.resource('s3')
s3_bucket = s3.bucket('Fin-app-ops')

file_in_path = 'data-ops/raw-d/'
file_dest_path = 'data-ops/con-d/'

Datafiles = [f.key for f in s3_bucket.objects.filter(prefix = file_in_path)]

for datafile in datafiles:
    if "xml" in obj.key:
        datafile = obj.get()['Body']
        data_dict = xmltodict.parse(datafile .read())
        datafile.close()
        json_data = json.dumps(data_dict)
        s3.Object(bucket_name, file_dest_path `enter code here`+'.json').put(Body=json.dumps(data_dict))

有其他方法可以实现这一目标,请帮助我是新手Python和Glue

I have my bucket structure like below and i have xml files landing in this s3 bucket folder.

S3:/Fin-app-ops/data-ops/raw-d

Need to convert those xml files to JSON files and put back to s3 in same bucket but different folder:
S3:/Fin-app-ops/data-ops/con-d

I tried by this way but did not work:

import os
import json
import boto3
import xmltodict

s3 = boto3.resource('s3')
s3_bucket = s3.bucket('Fin-app-ops')

file_in_path = 'data-ops/raw-d/'
file_dest_path = 'data-ops/con-d/'

Datafiles = [f.key for f in s3_bucket.objects.filter(prefix = file_in_path)]

for datafile in datafiles:
    if "xml" in obj.key:
        datafile = obj.get()['Body']
        data_dict = xmltodict.parse(datafile .read())
        datafile.close()
        json_data = json.dumps(data_dict)
        s3.Object(bucket_name, file_dest_path `enter code here`+'.json').put(Body=json.dumps(data_dict))

is there any other way I can achieve this, help please i'm new to Python and Glue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文