Python readline忽略csv文件中的标头

时间:2019-07-23 10:55:22

标签: python csv io readline

我写了一些代码,应该知道是否存在CSV文件,如果格式正确,可以追加到该文件中,或者删除它并创建一个新文件。

它检查格式是否正确的部分(通过检查CSV标头以查看它们是否相等)不起作用,因为出于某些奇怪的原因,readline()函数忽略了标头(应该是CSV文件的第一行。

请注意,我不想使用其他依赖项,例如熊猫。

    import os, csv

    path = 'intent_outputs.csv'
    response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
    output = open(path, 'a')
    csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
    writer = csv.DictWriter(output, fieldnames=csv_columns)
    if not os.path.exists(path):
        print("DOES NOT EXIST")
        output = open(path, 'w')
    else:
        print("EXISTS")
        output = open(path, 'r')
    if(output.readline() != ",".join(csv_columns)):
        print("NOT EQUAL")
        try:
            output = open(path, 'w')
            writer.writeheader()
        except IOError:
            print("IOError")
    else:
        print("EQUAL")
        output = open(path, 'a')
    try:
        row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
        writer.writerow(row)
    except IOError:
        print("I/O error") 

这里是 Johnny intent_outputs.csv

Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL
eggs,eggs,are tasty,VISIT PAGE,eggs.com,eggs,eggs.com

我想阅读第一行(以“ Query ....开头”),但正在被主动忽略。

2 个答案:

答案 0 :(得分:0)

readline()输出的末尾包含换行符\n。 因此output.readline()返回

Query,Header,Text,Button Text,Button URL,Context Button Text,Context Button URL\n

也许这就是为什么这种情况if(output.readline() != ",".join(csv_columns)):返回True的原因。

答案 1 :(得分:0)

最终自己解决了这个问题。因此,我重写了该函数,使其不依赖于打开的文件,除了上面代码中的一些额外的逻辑错误外,下面的脚本可以按预期工作。

@Suitsense的回答使我意识到\n最后添加的readline(),谢谢!!

    path = 'intent_outputs.csv'
    response = {'query':'eggs', 'header':'eggs', 'fulfillmentText':'are tasty', 'buttontext':'VISIT PAGE', 'buttonurl':'eggs.com', 'contextbuttontext':'eggs', 'contextbuttonurl':'eggs.com'}
    csv_columns = ['Query', 'Header', 'Text', 'Button Text', 'Button URL', 'Context Button Text', 'Context Button URL']
    output = ""
    writer = ""
    try:
        output = open(path,'x')
    except FileExistsError:
        output = open(path,'r')
    if(open(path,'r').readline() != ",".join(csv_columns)+"\n"):
        try:
            output = open(path, 'w')
            writer = csv.DictWriter(output, fieldnames=csv_columns)
            writer.writeheader()
        except IOError:
            print("IOError: Couldn't write header")
    else:
        output = open(path, 'a')
        writer = csv.DictWriter(output, fieldnames=csv_columns)
    row = {'Query':response['query'], 'Header':response['header'], 'Text':response['fulfillmentText'], 'Button Text':response['buttontext'], 'Button URL':response['buttonurl'], 'Context Button Text':response['contextbuttontext'], 'Context Button URL':response['contextbuttonurl']}
    writer.writerow(row)