【问题标题】:Header is repeating when merging multiple files using Python shell in AWS Glue在 AWS Glue 中使用 Python shell 合并多个文件时,标题重复
【发布时间】:2021-05-31 19:34:36
【问题描述】:

我是 Python 和 AWS Glue 的新手。

我正在尝试合并 S3 源存储桶中的几个 excel 文件并在目标 S3 存储桶中生成 1 个输出文件 (csv)。我能够读取并生成包含合并数据的输出文件,但唯一的问题是标题从每个文件中重复。

有人可以帮助调试以删除重复的标题吗?

下面是我的代码:

import pandas as pd
import glob
import xlrd
import openpyxl
import boto3
import io
import json
import os
from io import StringIO 
import numpy as np

s3 = boto3.resource('s3')
bucket = s3.Bucket('test bucket')
prefix_objs = bucket.objects.filter(Prefix='source/file')
prefix_df = []
for obj in prefix_objs:
key = obj.key
print(key)
temp = pd.read_excel(obj.get()['Body'], encoding='utf8')
prefix_df.append(temp)

bucket = 'test bucket'
csv_buffer = StringIO()
for current_df in prefix_df:
current_df.to_csv(csv_buffer, index = None)
print(current_df)

s3_resource = boto3.resource('s3')
s3_resource.Object(bucket, 'merge.csv').put(Body=csv_buffer.getvalue())

请帮忙!

问候, 维杰

【问题讨论】:

    标签: python-3.x amazon-s3 aws-glue


    【解决方案1】:

    更改此行并添加参数header

    temp = pd.read_excel(obj.get()['Body'], encoding='utf8')
    

    temp = pd.read_excel(obj.get()['Body'], encoding='utf8', header=1)
    

    temp = pd.read_excel(obj.get()['Body'], encoding='utf8', skiprows=1)
    

    您需要测试表头值,因为有时表头不在第一行开始。

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

    【讨论】:

    • 我在文档中添加了一些其他选项...作为可能对您有所帮助的更多内容
    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2017-12-26
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多