繁体   English   中英

在 AWS Glue 中使用 Python shell 合并多个文件时 Header 重复

[英]Header is repeating when merging multiple files using Python shell in AWS Glue

我是 Python 和 AWS Glue 的新手。

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

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

下面是我的代码:

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())

请帮忙!

问候,维杰

更改此行并添加参数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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM