简体   繁体   English

如何避免在python中的csv文件中重复“标题”..?

[英]How to avoid repetition of "Header" in csv file in python..?

I'm facing the problem of repetition of "header" in a csv file in python.我正面临在 python 中的 csv 文件中重复“标题”的问题。 When I execute the code, I got another csv file which I want so it's on the correct path but the problem is inside it which I showed in the result below:当我执行代码时,我得到了另一个我想要的 csv 文件,因此它位于正确的路径上,但问题出在其中,我在下面的结果中显示了它:

import csv
with open('url.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        url=row['URL']
        no_dots= url.count(".")
        no_per= url.count("%")
        no_dash= url.count("-")
        no_under= url.count("_")
        no_equal= url.count("=")
        no_question=url.count("?")
        no_colon=url.count(":")
        no_ampper=url.count("&")
        no_hash=url.count("#")
        no_dollar=url.count("$")
        no_atrate=url.count("@")
        no_exla=url.count("!")
        no_star=url.count("*")
        no_plus=url.count("+")
        no_hiphen=url.count("-")
        no_xor=url.count("^")
        no_slash=url.count("/")
        no_less=url.count("<")
        no_greater=url.count(">")


        with open('features.csv', 'a') as csvfile:
            fieldnames = ['dots', 'per','dash','under','equal','question','colon','ampper','hash','dollar','atrate','exla','star',
                          'plus','hiphen','xor','slash','less','greater','URL']
            writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
            writer.writeheader()



            writer.writerow({'dots': no_dots, 'per': no_per,'dash':no_dash,'under':no_under,
                                'equal': no_equal, 'question': no_question, 'colon': no_colon , 'ampper': no_ampper, 'hash': no_hash, 'dollar': no_dollar, 'atrate': no_atrate,
                                 'exla': no_exla, 'star': no_star , 'plus': no_plus,'hiphen': no_hiphen, 'xor': no_xor, 'slash': no_slash, 'less': no_less, 'greater': no_greater,
                                  'URL':url}

I'm expecting a result like:我期待这样的结果:

dots  per  dash under  equal  question  colon  ampper  hash....url
1     0     0    0      1        1        1       0      0     

https://stackoverflow.com/questions/ask?guided=true https://stackoverflow.com/questions/ask?guided=true

But I'm getting the result in repetition of header again and again for each url and I got more than 50000 url in my dataset... Please help me with the code.但是我得到的结果是对每个 url 一次又一次地重复标题,并且我的数据集中有超过 50000 个 url...请帮我写代码。 Thanks in advance.提前致谢。

import os

out_file = '/path/to/my/features.csv'

        if not os.path.isfile(out_file):            
            writer.writeheader()
            writer.writerow({'
        else:
            writer.writerow({'

You open your out file/dict in 'a' append mode, so check if the file exists, if it doesn't, write the header and row, if it does, just write the row.您以“a”附加模式打开输出文件/字典,因此请检查文件是否存在,如果不存在,则写入标题和行,如果存在,则只写入行。 HTH HTH

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

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