Python Forum
Step through a really large CSV file incrementally in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Step through a really large CSV file incrementally in Python
#2
Hope you will get some idea.

The following example assumes
CSV file contains column names in the first line
Connection is already built
File name is test.csv
Table name is MyTable
Python 3
with open ('test.csv', 'r') as f:
    reader = csv.reader(f)
    columns = next(reader) 
    query = 'insert into MyTable({0}) values ({1})'
    query = query.format(','.join(columns), ','.join('?' * len(columns)))
    cursor = connection.cursor()
    for data in reader:
        cursor.execute(query, data)
    cursor.commit()
If column names are not included in the file:

...
with open ('test.csv', 'r') as f:
    reader = csv.reader(f)
    data = next(reader) 
    query = 'insert into dbo.Test values ({0})'
    query = query.format(','.join('?' * len(data)))
    cursor = connection.cursor()
    cursor.execute(query, data)
    for data in reader:
        cursor.execute(query, data)
    cursor.commit()
Source :

https://stackoverflow.com/questions/2125...ing-python
Reply


Messages In This Thread
RE: Step through a really large CSV file incrementally in Python - by parthi1705 - May-06-2019, 07:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems writing a large text file in python Vilius 4 1,090 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Converted EXE file size is too large Rajasekaran 0 2,719 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  validate large json file with millions of records in batches herobpv 3 2,280 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Pyinstaller distribution file seems too large hammer 4 5,093 Mar-31-2022, 02:33 PM
Last Post: snippsat
  Initializing, reading and updating a large JSON file medatib531 0 2,872 Mar-10-2022, 07:58 PM
Last Post: medatib531
  can't read QRcode in large file simoneek 0 2,041 Sep-16-2020, 08:52 AM
Last Post: simoneek
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 9,266 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 2,608 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Loading large .csv file with pandas hangejj 2 3,395 Jun-08-2020, 01:32 AM
Last Post: hangejj
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 2,766 Jun-04-2020, 04:51 PM
Last Post: CoderMan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020