python3检查csv文件中数据是否准确

一段简短的小程序,用于检查csv文件中数据是否满足需求,最后不满足的数据组会被输出到同名xml文件中

#!/usr/bin/python

#CSVtoXML.py

#encoding:utf-8
import csv, os
from xml.dom.minidom import Document

#prfixFile = "creature_data"
def check_NSC_Res(NSC_ID,Res):#检查函数,可以根据自己需求描述
    flag = True
    for i in range(10):
        #print(i);
        #print(NSC_ID[i+1])
        #print(Res[16*3 + i*2 + 1])
        if(NSC_ID[i+1] != Res[16*3 + i*2 + 1]):
            flag = False
    #print(flag)
    return flag

def check_data(Chipid,NSC_ID,Res):
    if(Chipid in Res and check_NSC_Res(NSC_ID,Res)):
        return True
    else:
        return False

def checkXMLFile(filePrefix):
    csvFile = open(filePrefix+'.csv');
    headLine = csvFile.readline()
    #print headLine
    typeList = headLine.split(',')

    doc = Document()
    dataRoot = doc.createElement('ProdLogs')
    dataRoot.setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance")
    dataRoot.setAttribute('xsi:noNamespaceSchemaLocation', filePrefix+'.xsd')
    doc.appendChild(dataRoot)

    dataHeader = doc.createElement('Header')
    dataHeader.setAttribute('STBManufacturerName','Bedinsat')
    #dataHeader.setAttribute('STBTechnicalModelName','S1226A')
    #dataHeader.setAttribute('STBDistributorName','Bedinsat')
    #dataHeader.setAttribute('STBDistributorModelName','BS9900s')
    dataHeader.setAttribute('FormatVersion','01.00.00')
    dataRoot.appendChild(dataHeader)

    #csvReader = csv.reader(csvFile)
    #total = len(csvFile.readlines())
    dataLog = doc.createElement('Logs')
    dataLog.setAttribute('CreationDate','2021/03/30 10:51')
    
    total = 0
    csvReader = csv.reader(csvFile)
    for line in csvReader:
        #print line
        #total+=1
        dataElt = doc.createElement('LogRecord')
        for i in range(len(typeList)):
            typeList[i] = typeList[i].strip("\n")
            #print(typeList[i])
            if typeList[i] == ("ChipId"):
                #line[i] = line[i].replace(" ","")
                chipid = line[i]
                #print(line[i])
            if typeList[i] == ("CardlessSmartcardNumber"):
                line[i] = line[i].replace(" ","")
                NSC_ID = line[i]
                #print(line[i])
            if typeList[i] == ("Res"):
                res = line[i]
                #print(line[i])
            dataElt.setAttribute(typeList[i], line[i])
        if(check_data(chipid,NSC_ID,res) == False):
            total+=1
            print("not ok")
            dataLog.appendChild(dataElt)

    dataLog.setAttribute('NumberOfRecords',str(total))
    dataRoot.appendChild(dataLog)
    xmlFile = open(filePrefix+'.xml','w')
    xmlFile.write(doc.toprettyxml(indent = '\t'))
    xmlFile.close()

def main():
    for root, dirs, files in os.walk(os.getcwd()):
        for fname in files:
            index = fname.find('.csv')
            if index > 0:
                #print index, fname[:index]
                checkXMLFile(fname[:index])
                print("Transform " + fname + " OK!")

if __name__ == '__main__':
    main()
    input("Over!")
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值