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

前端 未结 2 1026
谎友^
谎友^ 2020-12-06 10:44

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

相关标签:
2条回答
  • 2020-12-06 11:41

    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.

    0 讨论(0)
  • 2020-12-06 11:44

    If you specify an nargs argument to .add_argument, the argument will always be returned as a list.

    Assuming you want to deal with all of the files specified, loop through that list:

    for filename in args.infile:
        for line in csv.reader(open(filename)): 
            for item in line[2:]:
    
                #to skip empty cells
    [...]
    

    Or if you really just want to be able to specify a single file; just get rid of nargs="+".

    0 讨论(0)
提交回复
热议问题