0

I have quite large CSV files containing data about case verdicts from courts. I'm trying to create a script that searches the whole CSV file, and if a word is found, the whole row should be copied to a new CSV file.

I managed to create a script that can do that if there is just one word in each "cell" of the CSV, which isn't the case for me.

This is the Python script I'm working on:

# -*- coding: utf-8 -*-
import sys
import csv
import re

writeFile = open('verdictsOutput.csv', 'wb')
writer = csv.writer(writeFile)

with open('TestDomstol.csv', 'r') as verdictFileInput:
    search = input("Enter keyword: ")
    verdictFileReader = csv.reader(verdictFileInput, delimiter=';')
    for row in verdictFileReader:
        for field in row:
            if field == search:
                writer.writerow(row)

The TestDomstol.csv looks like this (more than 1000 entries):

F1234;2019-09-22;Appeal over the decision bla bla, diaria number X regarding utility easement, compensation in Sweden;Utility easement;keyword

If I type in "keyword" in my Python script it works fine, because "keyword" is just one word in a cell. But what I want is to be able to type in and search for the word "compensation" (which is in the third column among a lot of other words).

Is there anyone who knows what changes need to be made? I have searched around on here, and Google, all morning, but I haven't been able to find some similar question or answer.

alex
  • 6,818
  • 9
  • 52
  • 103
Eric
  • 11

1 Answers1

0

Instead of parsing the csv file, iter over all columns and check if one of the field contains the keyword (you should use search in field instead of == as anupsabraham pointed out), you can also save some processing power here and check if the keyword is somewhere in the line and write the line as it is.:

# -*- coding: utf-8 -*-

in_file = 'TestDomstol.csv'
out_file = 'verdictsOutput.csv'
search = raw_input("Enter keyword: ")

with open(in_file, 'r') as verdictFileInput:
    header = verdictFileInput.next()
    with open(out_file, 'wb') as verdictFileOutput:
        verdictFileOutput.write(header)
        for line in verdictFileInput:
            if search in line:
                verdictFileOutput.write(line)
Jan Zeiseweis
  • 3,718
  • 2
  • 17
  • 24
  • Thank you! I'll try your solution as well. The previously asked question looked like it answered my problems here. Was just hard to find when you don't search for the correct terms. Anyway, my script works now, so can go on with the regular work :) Thanks a lot! – Eric Oct 12 '17 at 12:50