Python Forum

Full Version: Importing a CSV loops to infinity in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've accidentally created an infinite loop importing a CSV file using python and importing it into MySQL. I don't see where the error is!

This is my code:

import os
import mysql.connector
import csv
import pandas
source = os.path.join('source_files', 'aws_bills', 'march-bill-original-2019.csv')
destination = os.path.join('output_files', 'aws_bills', 'aws-bill-2019-03.csv')
mydb = mysql.connector.connect(user='xxxx', password='xxxxx',
                            host='xxxxx',
                            database='aws_bill')
cursor = mydb.cursor()
    try:
        with open(source) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            next(csv_reader)
            sql = "INSERT INTO billing_info (InvoiceId, PayerAccountId, LinkedAccountId, RecordType, RecordId, ProductName, RateId, SubscriptionId, PricingPlanId, UsageType, Operation, AvailabilityZone, ReservedInstance, ItemDescription, UsageStartDate, UsageEndDate, UsageQuantity, BlendedRate, BlendedCost, UnBlendedRate, UnBlendedCost, ResourceId, Engagement, Name, Owner, Parent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
            for row in csv_reader:
                print(row)
                try:
                    cursor.execute(sql.format(row))
                except Exception as e:
                    print("MySQL Exception: ", e)
                mydb.commit()
            print("Done importing data.")      
    except Exception as e:
        print("Exception:", e)
        mydb.rollback()
    finally:
        mydb.close()
The main field that I'm using to identify what is being inserted into the database is the one called LinkedAccountId.

It's a set of unique numbers that identify AWS accounts.

I'm verifying that the data is being inserted by logging into the database and watching it with this query:
select ID,LinkedAccountId from billing_info ORDER BY ID DESC LIMIT 25;
After a while of importing, I see that it starts again from the top of the CSV file. The LinkedAccountIds that were reported before start appearing again. Indicating that it has started over from the beginning of the file.

Why is this code looping infinitely???
(May-03-2019, 03:19 PM)bluethundr Wrote: [ -> ]Why is this code looping infinitely???
it's not looping infinitely, but every time you run it, it starts from the top of the file (i.e. imports records alraedy imported in previous runs.
(May-03-2019, 03:25 PM)buran Wrote: [ -> ]
(May-03-2019, 03:19 PM)bluethundr Wrote: [ -> ]Why is this code looping infinitely???
it's not looping infinitely, but every time you run it, it starts from the top of the file (i.e. imports records alraedy imported in previous runs.
To clarify, these are not separate runs of the script. I run the script once. It takes a while to import the data. Then after a while I start seeing the account ID's from the top of the file get inserted into the database again. Also the script is printing out each row as it inserts. And I see the IDs repeating there too.

I need to find out why that happens and correct it.

Sigh... it just happened again! I was running the script (once) and waiting for the file to import. Then after a while I start seeing this number that was input into the database previously: 051170381115. It's very frustrating! Just don't see why this file is looping this way.