Help in creating a dynamic/loop based on variables and CSV files

Peter Otten __peter__ at
Sun Dec 11 05:33:03 EST 2016

Umar Yusuf wrote:

> Hi all,
> I need your help with any of these questions?
> 1-

You should really make a serious attempt to explain the problem in plain 
english. Throwing a piece of code at other people is usually not enough to 
get a meaningful answer.

That said, my crystal ball tells me that you want to remove all rows of a 
csv before the one containing a keyword in the first column. You can do it 
for one keyword/csv-infile/csv-outfile triple,

> with open("keywords.txt", "rb") as keywords, open('folding umbrella-sort-
highest.csv', 'rb') as g, open('lego minecraft-sort-highest.csv', 'rb') as 
f, open('filename1.csv', 'wb') as myfile1, open('filename2.csv', 'wb') as 
> # Step1: Read contents of keywords.tex in variables
>     ky = list(keywords.readlines())
>     ky1, ky2 = ky[0], ky[1]
> # Step2: Read and process folding umbrella-sort-highest.csv
>     reader = csv.reader(g)
>     umbrella_list = list(reader)
>     list1 = filter(lambda e: e[0] in ky1, umbrella_list)
>     list2 = list(chain(*list1))
>     # or better: (available since Python 2.6)
>     # print list(chain.from_iterable(list1))
>     ind_prt1 = umbrella_list.index(list2) +1 
>     mylist1 = umbrella_list[:ind_prt1]
>     for r in mylist1:
>         wr = csv.writer(myfile1, quoting=csv.QUOTE_ALL)
>         wr.writerow(r)

and by employing copy-and-paste coding you have managed to do it for two 
keywords. Now you want to generalize it for an arbitrary number of triples.

The usual approach is to put the code that works for one instance into a 
function and call that for every set of arguments

def process_one(keyword, infile, outfile):
    # your code

for keyword, infile, outfile in triples:
    process_one(keyword, infile, outfile)

Where can you get those triples? Rather than putting just the keywords into 
a text file i would use a csv with three columns:

first_keyword,folding umbrella-sort-highest.csv,filename1.csv
second_keyword,lego minecraft-sort-highest.csv,filename2.csv

Then the calling code becomes

with open("keywords.csv", "rb") as f:
    for keyword, infile, outfile in csv.reader(f):
        process_one(keyword, infile, outfile)

Now to the contents of process_one(). Your code is very complicated. If it 
does what I think it can be rewritten as

def process_one(keyword, infile, outfile):
    with open(outfile, "wb") as outstream:
        writer = csv.writer(outstream, csv.QUOTE_ALL)
        with open(infile, "rb") as instream:
            for row in csv.reader(instream):
                if row[0] in keyword:

By the way, the

row[0] in keyword

test looks odd. Should that be

keyword in row[0]


keyword == row[0]

? Without a proper description of the problem that led to your code I have 
no way to tell.

> 2-
> Thanks in advance for your time.

More information about the Python-list mailing list