I am trying to upload a CSV file into a an SQL Server database using python.I am not able to handle new line characters.The file behaves differently in MS Excel and Noptepad++
The following is an example of CSV file that contains new line characters.The file looks like this in notepad++
but it looks like this in excel.
The text breaks into two parts in column C.
I tried to handle newline characters like this
wfile = open(UploadFile, "rU")
reader = csv.reader(wfile,delimiter = ",",dialect='excel')
with open(UploadFile, "r") as uploadData:
formatter_string = "%d/%m/%y %H:%M"
for row in reader:
datetime_object = datetime.strptime(row[9], formatter_string)
row[9] = str(datetime_object.date())
cursor.execute("insert into "+UploadTable+" values ("+(row[9])+","+(row[0])+","+(row[1])+","+(row[2])+","+(row[8])+","+(row[3])+","+(row[4])+","+(row[5])+","+(row[6])+","+(row[7])+")")
I read this here When I tried to upload this file I got this error
Failed to upload Facebook data.
list index out of range
I am not sure what is going wrong.
stack trace:
Traceback (most recent call last):
File "G:/P/14. Digital metrics - Phase 2/3. Execution/4. Code/Code Final/Unmetric Post level/test.py", line 66, in <module>
FBPostupload(os.getcwd()+'\FB_Unm_postlevel_camp_mapping.csv','Unm_Fb_Posts_Stage1_test')
File "G:/P/14. Digital metrics - Phase 2/3. Execution/4. Code/Code Final/Unmetric Post level/test.py", line 53, in FBPostupload
datetime_object = datetime.strptime(row[9], formatter_string)
IndexError: list index out of range