0

I am trying to classify articles by categories using the regex module. Here's the code I use to classify :

import re

def classify(txt):
    prio_list = ["category 1", etc.]
    matchers = {"category 1": "[Cc]ategory 1", etc.}
    for category in prio_list:
        count = 0
        for match in re.findall(matchers[category], txt):
            count += 1
        if re.findall(matchers[category], txt):
            print (category)

I now want to write each result on its corresponding line, in a new column.

Input I have :

file.csv

col 1, col 2, col 3

1, 2, 3

Output I want :

file.csv

col 1, col 2, col 3, col 4

1, 2, 3, category

How can I complete this line of code in order to do so? :

import csv

with open('file.csv', encoding="utf8") as f:
    writer = csv.writer(f)
    for index, line in enumerate(f): 
        classify(line)

0 Answers0