簡體   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