Home > OS >  Get Values from CSV to pass as Arguments in Array/List (Python)
Get Values from CSV to pass as Arguments in Array/List (Python)

Time:11-28

I would like to find values from one CSV in another and modify/remove the rows accordingly.

Removing already works quite well, but I would like to automate this process as much as possible.

So my question is how can I put all values from the serachforthat.csv (column [0]) into a kind of array or list and use it to run through the all.csv.

what i got so far:

*args = "searchforthat.csv[0]" # These are my values

import csv
with open('all.csv', 'r') as inp, open('final.csv', 'w') as out:
writer = csv.writer(out)
for row in csv.reader(inp):
    if row[3] != args: # That does not work :(
        writer.writerow(row)

I am completely new to python and a little confused as to the correct way to write it...

CodePudding user response:

You have to get the values in the first column of searchforthat.csv out of the file first.

search_values = []
with open('searchforthat.csv', 'r') as sfile:
    lines = [line.replace("\n", "") for line in sfile.readlines()]
    search_values = [line.split(",")[0] for line in lines]

Good Luck! Then you can use the strings in search_values to look through the other file(s).

It looks like in your question you want to compare the search_values with the values in column 0 of all.csv . And if there in there, write the lines to a new file. If that is the case you can do so like this:

# get the row data from searchforthat.csv
search_row_data = []
with open('searchforthat.csv', 'r') as sfile:
    lines = [line.replace("\n", "") for line in sfile.readlines()]
    search_row_data = [line.split(",") for line in lines]


# map search data to values
search_values = [row_data[0] for row_data in search_row_data]

# get the row data from all.csv
all_row_data = []
with open('all.csv', 'r') as afile:
    lines = [line.replace("\n", "") for line in afile.readlines()]
    all_row_data = [line.split(",") for line in lines

# go over all.csv values and if it is not it searchforthat.csv, write it.
with open('out.csv', 'w') as ofile:
    for row_data in all_row_data: 
        if row_data[3] not in search_values:
            ofile.write(','.join(row_data)   '\n')

CodePudding user response:

import csv

with open('searchforthat.csv', 'r') as inp:
  args = [row[0] for row in csv.reader(inp)]

with open('all.csv', 'r') as inp, open('final.csv', 'w') as out:
  writer = csv.writer(out)
  for row in csv.reader(inp):
      if row[3] not in args:
          writer.writerow(row)
  • Related