如何检查CSV文件的第一行是否已写入?

时间:2015-07-09 15:51:49

标签: python python-2.7

所以我每天都在运行这个Python脚本,但我希望它检查标题行是否已经写入,如果不是,则写入,如果不是则跳过它。我已经尝试过如第一行的读取和设置变量(如果有输入),但它没有工作。这是我的代码:

def addDomainsToFile(domainList):
    date = time.strftime("%d:%m:%Y")
    fileName = 'MagDomains%s.csv' % date
    #Create file with the date as the name, this should be a week to week file, check if day is monday, if so,

    with open(fileName, 'ab+') as c:
        writer = csv.writer(c ,dialect= 'excel', delimiter= ',')
        for row in fileName:
            print row
        writer.writerow(['domain','ip','identifier','relationship', 'related To'])
        for item in domainList:
            writer.writerow([item, '', 'Related'])

4 个答案:

答案 0 :(得分:2)

如何检查csv的文件大小是否大于零? 应该足够进行初步检查:

import os
if os.path.getsize(fileName) == 0:
    write_header()

答案 1 :(得分:1)

查看csv.Sniffer.has_header是否适合你。

https://docs.python.org/2/library/csv.html#csv.Sniffer

答案 2 :(得分:1)

您可以使用SELECT "MASTERS"."MEMBNAME", "MASTERS"."MEMBID", "MASTERS"."OPT", "MASTERS"."HPCODE", "MASTERS"."CLAIMNO", "MASTERS"."CROSSREF_ID", "DETAILS"."FROMDATESVC", "DETAILS"."TODATESVC", "MASTERS"."ADMDATE", "MASTERS"."DSCHDATE", "MASTERS"."DATERECD", "DETAILS"."DIAGCODE", "DIAG_CODES"."DIAGDESC", "MASTERS"."PLACESVC", "DETAILS"."PROCCODE", "DETAILS"."HSERVICECD", "DETAILS"."PROCDESC", "DETAILS"."HSERVICEDESC", "P_MASTERS"."FULLNAME", "V_MASTERS"."VENDORNM", "MASTERS"."SPEC", "P_MASTERS"."CLASS", "DETAILS"."BILLED", "DETAILS"."CONTRVAL", "DETAILS"."ADJUST", "DETAILS"."NET", "DETAILS"."INTEREST", "DETAILS"."QTY", "DETAILS"."ADJCODE", "MASTERS"."COMPANY_ID", "MEMB_COMPANY_V"."BIRTH", "ADJUST_CODES_V"."DESCR", "MEMB_COMPANY_V"."SEX", "P_MASTERS_1"."REV_FULLNAME", "MEMB_COMPANY_V"."OPFROMDT", "MEMB_COMPANY_V"."OPTHRUDT", "V_MASTERS"."VENDORID", "P_MASTERS"."CONTRACT", "ME_V"."MEMOLINE1", "DETAILS"."COPAY", "DETAILS"."SEQUENCE", "DETAILS"."DATEPAID", "DETAILS"."CHECKNO", "P_MASTERS_1"."ACCOUNT", "MASTERS"."ADMTYPE", "MASTERS"."ADMSOURCE", "MASTERS"."CONTRVAL", "MASTERS"."STATUS", "MASTERS"."DATEPAID", "MASTERS"."CHPREFIX", "MASTERS"."NET" FROM ((((((("Datawarehouse"."dbo"."MASTERS" "MASTERS" INNER JOIN "Datawarehouse"."dbo"."DETAILS" "DETAILS" ON "MASTERS"."CLAIMNO"="DETAILS"."CLAIMNO") INNER JOIN "Datawarehouse"."dbo"."V_MASTERS" "V_MASTERS" ON "MASTERS"."VENDOR"="V_MASTERS"."VENDORID") INNER JOIN "Datawarehouse"."dbo"."P_MASTERS" "P_MASTERS" ON ("MASTERS"."COMPANY_ID"="P_MASTERS"."COMPANY_ID") AND ("MASTERS"."PROVID"="P_MASTERS"."PROVID")) INNER JOIN "Datawarehouse"."dbo"."MEMB_COMPANY_V" "MEMB_COMPANY_V" ON ("MASTERS"."COMPANY_ID"="MEMB_COMPANY_V"."COMPANY_ID") AND ("MASTERS"."MEMBID"="MEMB_COMPANY_V"."MEMBID")) LEFT OUTER JOIN "Datawarehouse"."dbo"."ME_V" "ME_V" ON ("MASTERS"."CLAIMNO"="ME_V"."CLAIMNO") AND ("MASTERS"."COMPANY_ID"="ME_V"."COMPANY_ID")) INNER JOIN "Datawarehouse"."dbo"."DIAG_CODES" "DIAG_CODES" ON "DETAILS"."DIAGCODE"="DIAG_CODES"."DIAGCODE") LEFT OUTER JOIN "Datawarehouse"."dbo"."ADJUST_CODES_V" "ADJUST_CODES_V" ON "DETAILS"."ADJCODE"="ADJUST_CODES_V"."CODE") LEFT OUTER JOIN "Datawarehouse"."dbo"."P_MASTERS" "P_MASTERS_1" ON ("MEMB_COMPANY_V"."COMPANY_ID"="P_MASTERS_1"."COMPANY_ID") AND ("MEMB_COMPANY_V"."PCP"="P_MASTERS_1"."PROVID") WHERE ("MASTERS"."STATUS"='9' AND "MASTERS"."COMPANY_ID"='LWDLOM' AND ("DETAILS"."ADJCODE" IS NULL OR NOT ("DETAILS"."ADJCODE" LIKE 'D%' OR "DETAILS"."ADJCODE" LIKE 'KILL%')) AND "DETAILS"."NET"=0 AND "P_MASTERS"."CLASS"='51' AND ("MASTERS"."HPCODE"='GSMH' OR "MASTERS"."HPCODE"='HENS' OR "MASTERS"."HPCODE"='SCAS') AND "MASTERS"."CONTRVAL"<>0 OR "MASTERS"."CHPREFIX"=2 AND "MASTERS"."STATUS"='9' AND "MASTERS"."COMPANY_ID"='LWDLOM' AND ("DETAILS"."ADJCODE" IS NULL OR NOT ("DETAILS"."ADJCODE" LIKE 'D%' OR "DETAILS"."ADJCODE" LIKE 'KILL%')) AND "P_MASTERS"."CLASS"<>'51' AND ("MASTERS"."HPCODE"='GSMH' OR "MASTERS"."HPCODE"='HENS' OR "MASTERS"."HPCODE"='SCAS') AND "MASTERS"."NET"<>0) AND ("DETAILS"."FROMDATESVC">={ts '2014-01-01 00:00:00'} AND "DETAILS"."FROMDATESVC"<{ts '2015-12-31 00:00:01'}) AND "MASTERS"."DATEPAID"<{ts '2015-05-31 00:00:01'} ORDER BY "MASTERS"."CLAIMNO", "DETAILS"."SEQUENCE" csv功能阅读csv.reader的第一行,并与第一行进行比较:

next

答案 3 :(得分:0)

我也需要这样做,并且必须对Kasramvd的解决方案进行一些更改才能使其正常工作。 使用“a +”模式时,文件指针位于末尾。所以你必须跳转到文件的开头才能读取第一行。 读取标题后(如果有),您可以跳回到末尾以附加到文件。

with open(filename, 'a+') as f:  # Just use 'w' mode in 3.x
    logger.info("Opened file")
    f.seek(0)  # jump to the beginning of the file
    try:
        header = csv.reader(f).next()
        dict_writer = csv.DictWriter(f, header)  # header found
    except StopIteration:  # no header found
        dict_writer = csv.DictWriter(f, my_dict.keys())
        dict_writer.writeheader()
    f.seek(0,2)  # jump back to the end of the file
    try:
        dict_writer.writerow(my_dict)
    except ValueError:
        # some keys from my_dict are not in header