S3 バケットに CSV ファイル (カンマ区切り) があります。カンマを含むフィールドはほとんどなく、CSV ファイルは次のようになります。
Q,W,E,R
A,S,"D,F",G
Z,X,C,V
でこれを読むと、1 列pandas
で 4 列を取得する必要が"D,F"
ありますが、余分な列を取得しています。
私のコード; 私が試したさまざまなことですが、すべての試みはうまくいきませんでした:
import io
import csv
import pandas as pd
#encoding
result = chardet.detect(self.raw_content)
self.encoding = result['encoding']
#csv_delimiter
is being read from the DB ( , in this case)
#max_columns
is NUMBER of columns in the csv file
#reading from s3 bucket
self.raw_content = obj['Body'].read()
content = io.BytesIO(self.raw_content)
#Try 1
df_s3_file = pd.read_csv(content, delimiter=csv_delimiter, engine='python',
dtype=object, encoding=self.encoding, quotechar='"',
names=list(range(0,max_columns)))
#Try 2
df_s3_file = pd.read_csv(content, delimiter=csv_delimiter, engine='python',
dtype=object, encoding=self.encoding, quoting=csv.QUOTE_ALL,
names=list(range(0,max_columns)))
#Try 3
df_s3_file = pd.read_csv(content, delimiter=csv_delimiter, dtype=object,
encoding=self.encoding, quoting=csv.QUOTE_ALL,
names=list(range(0,max_columns)))
現在の結果:
0 1 2 4 5
Q W E R NaN
A S "D F" G
Z X C V NaN
期待される結果:
0 1 2 4
Q W E R
A S D,F G
Z X C V