0

I am trying to get a .csv file to load in a database I created.

Here is my function to load the data, where path is for the path of where the file is

def load_data(path,table_name,conn):

""" 
Populate table from data in csv file at 
provided path
"""
columns = get_table_cols(table_name,conn)
coma_seperated_columns = ",".join(columns)
cursor = conn.cursor()

with open(path) as csv_file:
    reader = csv.reader(csv_file, delimiter=',')
    for row in reader:
        values = ",".join(row)
        cursor.execute(f"INSERT INTO {table_name} 
       ({coma_seperated_columns}) VALUES ({values})")'

Here is how I call it:

  #I have the same file at two different place because I read that the error could be coming from the 
  #fact the file is not in OneDrive
  This is line 27:   load_data(r"C:\Users\18195\Desktop\Spring 2021\CS 
         205\warmUp\State.csv","StateDatabase",conn)

  #I also tried this:
  This is line 27: load_data(r"C:\Users\18195\OneDrive\State.csv",
         "StateDatabase",conn)

I get this as an error:

Traceback (most recent call last):
   File "C:/Users/18195/Desktop/Spring 2021/CS 
         205/warmUp/main.py", line 27, in <module>
      Database.load_data(r"C:\Users\18195\Desktop\Spring 2021\CS 
                           205\warmUp\State.csv","StateDatabase",conn)

When I run the debug, I can see that what I initialize for path value is not the same once in the function:

this

"C:\Users\18195\Desktop\Spring 2021\CS 205\warmUp\State.csv"

to this enter image description here

Why does it add a \ and is that what is causing the error? if not any idea why I am getting this error?

Thank you!

  • 1
    Aren't those 2 paths exactly the same? – SuperStormer Feb 28 '21 at 02:28
  • \\ is \ "escaped" https://en.wikipedia.org/wiki/Escape_character https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters, however, your question has nothing to do with paths and will confuse future readers, please retract it / create a minimal question involving a small string and an appropriate title... – ntg Mar 07 '21 at 17:40
  • I’m voting to close this question because the title does not correspond to the text, which is not a minimal (or even has effort to present a minimized version of the problem ) – ntg Mar 07 '21 at 17:42

0 Answers0