简体   繁体   English

新行作为CSV文件中的分隔符

[英]new line as delimiter in CSV file

I want to read a csv file till it finds a empty/new line I am using END as a delimiter, want to change it to new/empty line 我想读取一个csv文件,直到找到一个空行/换行,我正在使用END作为分隔符,想将其更改为新行/空行

 def getActualData(ws_name, ws_value):

    outputDatafile = open('D:\\Files\\Actual_Data.csv', 'r',newline="\n")
    outputReader = csv.reader(outputDatafile, delimiter=',', quoting=csv.QUOTE_NONE)

    outputData = {}
    check =0

    for rows in outputReader:
        #print "Row first:", rows[0]," Row second:",rows[1]
        if(check==0):    
            if(rows[0]==ws_name and rows[1]==ws_value):
                check+=1
        elif (rows[0]=='\n' and rows[1]=='\n'):
            break
        else:
            outputData.update({rows[0]:rows[1]})

if the length of rows is 0, it means empty line. 如果行的长度为0,则表示空行。 and you should check empty line first. 您应该先检查空白行。 otherwise, rows[0] can make exception. 否则,rows [0]可能会导致异常。

if (len(rows)==0):
    break    
elif(check==0):
    if(rows[0]==ws_name and rows[1]==ws_value):
        check+=1
else:
    outputData.update({rows[0]:rows[1]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM