python csv writter添加了额外的引号

时间:2016-05-10 13:33:55

标签: python csv quotes

我正在使用csv writter来创建txt文件。 合并列表时会添加额外的引号。 (分隔符\ t)

插入前的最终列表是

['20160501-20160531', '719', '1-4', '(1\t1\t12:00-19:00\t1)', '(1\t1\t12:00-19:00\t1)&(2\t1\t01:00-06:00\t2)']

写到文件时我得到了这个

20160501-20160531   719 1-4 "(1 1   12:00-19:00 1)" "(1 1   12:00-19:00 1)&(2   1   01:00-06:00 2)"

为什么有额外的报价?

一切都很好(制表符分隔和全部)除了额外的引号导致问题

我尝试使用quoting = csv.QUOTE_NONE但我只收到了未设置的escapechar的错误

这是我的代码

 for rec in formJson['presentDays']:
        period = '%s-%s'% (scheduleS, scheduleE)
        if not rec['workers']:
            continue
        else:
            workers = ' '.join(str(e) for e in rec['workers'])
            for rr in rec['data']:
                range = '%s-%s' %(rr['loadFrom'],rr['loadTo'])
                #prva vrstica v subdata je condition
                request = rr['subdata'][0]
                request_days = ' '.join(str(x) for x in request['days'])
                request_works = str(request['works'])
                request_time = '%s-%s' %(request['timeFrom'],request['timeTo'])
                request_how = str(request['how'])
                request_row = "(" + request_days + '\t'+ request_works + '\t'+ request_time + '\t'+ request_how + ")"

                #gremo vse nasledne zapise, ki so condition
                itersubdata = iter(rr['subdata'])
                next(itersubdata)
                conditions_rows = []
                for r in itersubdata:
                    condition_days = str(r['days'][0])
                    condition_works = str(r['works'])
                    condition_time = '%s-%s' %(r['timeFrom'],r['timeTo'])
                    condition_how = str(r['how'])
                    condition_row = '(%s\t%s\t%s\t%s)' % (condition_days, condition_works, condition_time, condition_how)
                    conditions_rows.append(condition_row)
                conditions = '&'.join(x for x in conditions_rows)
                record = [period, workers,range,str(request_row), conditions]
                print "record ", record
                output.writerow(record)

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

看起来您需要包含空格的字符串的引号(在这种情况下为制表符)。