Dr.Viper
Dr.Viper

Reputation: 411

python3 list subtract issue - error list is not callable

I am trying to open csv files (two) and subtract values from two columns from those two files. I call the data into arrays, and then use a map and operator.sub to get this, but I am getting stuck on outputting that data as print or as another csv.

I have data in the form of two columns - a1, b1 and a2, b2 in two files. I want to find subtract b2(i) and b1(i), and make a new csv file with the difference "b". Data values are a bit large to copy here. For example, 1,10, /n 2, 15 /n 3 20, etc. and 1,20 /n 2,20 /n 3, 30. I should get 5, 5, 10 as output list or as an array or even as an output file.

My problem - I am not getting any output, but error saying "list" is not callable. I searched through a lot of details on the built-in function matter, but still don't know where I am messing up.

import csv
try:
    from itertools import imap
except ImportError:
    # Python 3...
    imap=map
from operator import sub

a = []
b = []
c = []

with open('1.csv') as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        a.append(row[1])

with open('2.csv') as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        b.append(row[1])

c = list(imap(sub, a, b))

print(c)

Upvotes: 0

Views: 51

Answers (1)

user6172474
user6172474

Reputation:

Just create an output file and put data inside

You may use pandas or just current csv lib

with open('my_output.csv', mode='w') as output_file:
    writer = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    writer.writerow(['John Smith', 'Accounting', 'November'])

Upvotes: 1

Related Questions