TypeError: coercing to Unicode: need string or buffer, list found

argparse, python, python-2.7

Solution

`args.infile` is a list of filenames, not one filename. Loop over it:

for filename in args.infile:
    base, ext = os.path.splitext(filename)
    with open("{}1{}".format(base, ext), "wb") as outf, open(filename, 'rb') as inf:
        output = csv.writer(outf) 
        for line in csv.reader(inf): 

Here I used `os.path.splitext()` to split extension and base filename so you can generate a new output filename adding `1` to the base.

Problem

I'm trying to get a data parsing script up and running. It works as far as the data manipulation is concerned. What I'm trying to do is set this up so I can enter multiple user defined CSV's with a single command. e.g. ``` > python script.py One.csv Two.csv Three.csv ``` If you have any advice on how to automate the naming of the output CSV so that if `input = test.csv`, `output = test1.csv`, I'd appreciate that as well. Getting ``` TypeError: coercing to Unicode: need string or buffer, list found ``` for the line ``` for line in csv.reader(open(args.infile)): ``` My code: ``` import csv import pprint pp = pprint.PrettyPrinter(indent=4) res = [] import argparse parser = argparse.ArgumentParser() #parser.add_argument("infile", nargs="*", type=str) #args = parser.parse_args() parser.add_argument ("infile", metavar="CSV", nargs="+", type=str, help="data file") args = parser.parse_args() with open("out.csv","wb") as f: output = csv.writer(f) for line in csv.reader(open(args.infile)): for item in line[2:]: #to skip empty cells if not item.strip(): continue item = item.split(":") item[1] = item[1].rstrip("%") print([line[1]+item[0],item[1]]) res.append([line[1]+item[0],item[1]]) output.writerow([line[1]+item[0],item[1].rstrip("%")]) ``` I don't really understand what is going on with the error. Can someone explain this in layman's terms? Bear in mind I am new to programming/python as a whole and am basically learning alone, so if possible could you explain what is going wrong/how to fix it so I can note it for future reference.

Original source