Я сталкиваюсь с проблемой повторения "заголовка" в CSV-файле в Python. Когда я выполняю код, я получаю другой файл 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}
Я ожидаю такой результат, как:
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
Но я получаю результат повторения заголовка снова и снова для каждого URL, и я получил более 50000 URL в моем наборе данных ...
Пожалуйста, помогите мне с кодом. Заранее спасибо.