26Cocktails
26Cocktails

Reputation: 53

Insert statement returning 'object has no attribute 'uuid'

I'm working to insert some code into sql server and I keep running into the following error:

{
  "errorMessage": 'str' object has no attribute 'uuid'"
  "errorType": AttributeError",
  "stackTrace": [
   File \"/var/task/data_insert/clean_sweep.py\", line 17 , row.uuid\n"
  ]
}

My code looks like:

client = boto3.client('s3')
my_bucket = 'data-staging'
data_filename = 'data_pull.csv'
insert_csv = client.get_object(Bucket = my_bucket, Key = data_filename)
data_body = insert_csv["body"]
csv_string = data_body.read().decode('utf-8')
data_df = pd.read_csv(StringIO(csv_string))

conn = pymssql.connect(server = 'rds_sqlserver.com', user='etl_xu',password = 'XU2014basketball', database = 'Xacation')
cursor = conn.cursor()
for row in data_df.itterrows():
cursor.execute('''INSERT INTO xac.staging(uuid, last_name, xac_account)
                        VALUES(?,?,?)
                        '''
                        , row.uuid
                        , row.last_name
                        , row.xac_account
                        )

I thought skipping over the first row due to it being headers would fix it but it's really not the case. Help please!

Upvotes: 2

Views: 522

Answers (1)

lynkfox
lynkfox

Reputation: 2400

I'm fairly sure this is the source of your problem:

csv_string = data_body.read().decode('utf-8')
data_df = pd.read_csv(StringIO(csv_string)) 

S3 objects Bodies are returned as a stream. You are reading the stream into an object and then attempting to re stream it with the StringIO -

I stream in XML files from s3 into xml.etree.ElementTree.fromstring() all the time which expects some sort byte stream or string - and simply use:

xml_s3_object = s3.get_object(Bucket=bucket_name, Key=object_key)['Body'].read()
tree = ET.fromstring(xml_s3_object)

So because you are decoding it and then restreaming it with StringIO you are likely causing some encoding/decoding errors that are causing issues in your rows.

Upvotes: 1

Related Questions