拆分哈希并将其写入python中的csv文件

时间:2016-04-06 10:50:24

标签: python csv hash

我正在尝试拆分哈希并将其写入不同的csv文件,但它会抛出错误

def split(self,filehandler, delimiter=',', row_limit=1000,
            output_name_template='output_%s.csv', output_path='.', keep_headers=True):

        #reader = csv.reader(filehandler, delimiter=delimiter)
        current_piece = 1
        current_out_path = os.path.join(
            output_path,
            output_name_template % current_piece
        )
        current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=',')
        current_limit = row_limit
        if keep_headers:
            headers = filehandler.next()
            current_out_writer.writerow(headers)
        for i, row in enumerate(filehandler):
            if i + 1 > current_limit:
                current_piece += 1
                current_limit = row_limit * current_piece
                current_out_path = os.path.join(
                    output_path,
                    output_name_template % current_piece
                )
                current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=',')
                if keep_headers:
                    current_out_writer.writerow(self.headers)
            current_out_writer.writerow(row)

split(self.merged_data)

第一个错误

 headers = filehandler.next()
AttributeError: 'dict' object has no attribute 'next'

第二个错误

for i, row in enumerate(filehandler):
TypeError: 'Merge_project' object is not iterable

如何修复错误并拆分哈希并写出不同的csv文件?

0 个答案:

没有答案