TypeError: coercing to Unicode: need string or buffer, list found
Question: trying to get this data parsing script up and running. It works as far as the
data manipulation is concerned, what im 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 
    

Also if you have any advice on how to automate the naming of the output CSV so
that if `input = test.csv`, `output = test1.csv`

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, so if somone could
explain in laymans?

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 so I can not it down for future reference


Answer: `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.

