У меня есть файл, и мне нужно создать из него новый файл, который будет включать только два столбца для целей сопоставления. Я не могу использовать pandas, поскольку там, где я нахожусь, это запрещено, и я не могу понять, как поместить эти два поля в новый файл. Код, который у меня есть, приведен ниже. Я знаю, что итератор неполный. Я не уверен, как ограничить буфер этими двумя полями? Я был бы очень признателен за любые советы, которые кто-либо может дать. Большое спасибо:
# Read CSV from S3
response = s3_client.get_object(Bucket=bucket_name, Key=input_key)
input_data = response['Body'].read().decode('utf-8')
# Process the CSV
reader = csv.DictReader(StringIO(input_data))
rows = list(reader)
# Read all rows into memory
fieldnames = reader.fieldnames # Get the existing column names
# just get the two column values for the new file
for row in rows:
row["file_token"]
row["Payment reference"]
# Write the new CSV data to a string buffer
output_buffer = StringIO()
writer = csv.DictWriter(output_buffer, fieldnames=fieldnames)
writer.writeheader() # Write the updated header
writer.writerows(rows) # Write the updated rows
output_buffer.seek(0) # Reset buffer pointer to the beginning
s3_client.put_object(Bucket=bucket_name, Key=output_key, Body=output_buffer.getvalue())
print(f"Updated file saved to s3://{bucket_name}/{output_key}")
Подробнее здесь: https://stackoverflow.com/questions/792 ... er-columns
Создать файл из другого файла, но с меньшим количеством столбцов. ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение