使用python处理CSV中的新行字符

时间:2014-07-18 09:04:23

标签: python sql-server excel csv

我正在尝试使用python将CSV文件上传到SQL Server数据库。我无法处理换行符。文件在MS Excel和Noptepad ++中的行为有所不同

以下是包含换行符号的CSV文件的示例。文件在notepad ++ this中看起来像这样但在excel this中看起来像这样。

该文本在C栏分为两部分。

我尝试处理像这样的换行符

 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])+")")

我读了这个here 当我尝试上传此文件时出现此错误

Failed to upload Facebook data.
list index out of range

我不确定会出现什么问题。

堆栈跟踪:

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

0 个答案:

没有答案