2

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++this
but it looks like this in excelthis.

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
Community
  • 1
  • 1
Raki
  • 41
  • 7
  • Please post the **full** traceback - your reader code is ok (you can even get rid of the keyword args FWIW, they are the default), so the problem is somewhere else. – bruno desthuilliers Jul 18 '14 at 09:35
  • Add a try/except block around line 53 to catch the `IndexError`, and in the except clause print the offending row and `reader.line_num` (and reraise the exception...). There's something wrong with your file but I had no problem with the newlines here. – bruno desthuilliers Jul 18 '14 at 10:18
  • `try: datetime_object = datetime.strptime(row[9], formatter_string) row[9] = str(datetime_object.date()) except IndexError,i: print "Index error occured" print row print reader.line_num raise Exception(IndexError)` This did not fix the issue. – Raki Jul 18 '14 at 10:49
  • Err... It's (obviously) **not** supposed to "fix" anything, just to let you know what the offending row looks like and which line of the csv file this row comes from... – bruno desthuilliers Jul 18 '14 at 11:49

0 Answers0