Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免“重复”;标题“;在python中的csv文件中。。?_Python_Excel - Fatal编程技术网

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

如何避免“重复”;标题“;在python中的csv文件中。。?,python,excel,Python,Excel,我面临着在python中csv文件中重复“header”的问题。当我执行代码时,我得到了另一个我想要的csv文件,因此它位于正确的路径上,但问题在于它内部,我在下面的结果中显示了这一点: 导入csv 将open('url.csv')作为csvfile: reader=csv.DictReader(csvfile) 对于读取器中的行: url=行['url'] 无点=url.count(“.”) no_per=url.count(“%”) no_dash=url.count(“-”) =url.c

我面临着在python中csv文件中重复“header”的问题。当我执行代码时,我得到了另一个我想要的csv文件,因此它位于正确的路径上,但问题在于它内部,我在下面的结果中显示了这一点:

导入csv
将open('url.csv')作为csvfile:
reader=csv.DictReader(csvfile)
对于读取器中的行:
url=行['url']
无点=url.count(“.”)
no_per=url.count(“%”)
no_dash=url.count(“-”)
=url.count(“\u”)下没有\u
不等于url.count(“=”)
no_question=url.count(“?”)
no_colon=url.count(“:”)
否\u 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\u hiphen=url.count(“-”)
no_xor=url.count(“^”)
无斜杠=url.count(“/”)
no_less=url.count(“”)
将open('features.csv','a')作为csvfile:
字段名=['dots'、'per'、'dash'、'under'、'equal'、'question'、'ampper'、'hash'、'dollar'、'atrate'、'exla'、'star',
‘加号’、‘hiphen’、‘xor’、‘斜杠’、‘少’、‘大’、‘URL’]
writer=csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'dots':no_dots,'per':no_per,'dash':no_dash,'under':no_under,
“相等”:不等号,“问题”:不等号,“冒号”:不等号,“安培”:不等号,“哈希”:不等号,“美元”:不等号,“atrate”:不等号,
“exla”:不,exla,'star':不,star,'plus':不,plus,'hiphen':不,hiphen,'xor':不,xor,'slash':不,slash,'less':不,less,'greater':不,greater,',
“URL”:URL}
我期待的结果是:

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

但我得到的结果是对每个url重复一次又一次的标题,我的数据集中有50000多个url。。。 请帮我输入密码,谢谢

import os

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

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

您在“a”追加模式下打开文件/dict,检查文件是否存在,如果不存在,请写标题和行,如果存在,请写行。HTH

您一直使用的标记不适用于此问题。请阅读、查看和发布。请记住,在询问问题时,至少要阅读您正在使用的标记上的鼠标盖问题