将txt转换为xlsx时出现MemoryError

将txt转换为xlsx时出现MemoryError

本文介绍了将txt转换为xlsx时出现MemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题:1. 使用python将txt转换为xlsx时出错

  1. 将数字单元格的单元格属性设置为数字时,将txt转换为xlsx

我的代码是

    import csv
    import openpyxl

    import sys


    def convert(input_path, output_path):
        """
        Read a csv file (with no quoting), and save its contents in an excel file.
        """
        wb = openpyxl.Workbook()
        ws = wb.worksheets[0]

        with open(input_path) as f:
            reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
            for row_index, row in enumerate(reader, 1):
                for col_index, value in enumerate(row, 1):
                    ws.cell(row=row_index, column=col_index).value = value
        print 'hello world'

        wb.save(output_path)

        print 'hello world2'


    def main():
        try:
            input_path, output_path = sys.argv[1:]
        except ValueError:
            print 'Usage: python %s input_path output_path' % (sys.argv[0],)
        else:
            convert(input_path, output_path)


    if __name__ == '__main__':
        main()

除某些输入文件外,此代码有效.我找不到导致此问题的输入txt与没有引起输入问题的txt之间的区别.

This code works, except for some input files. I couldn't find what the difference is between the input txt that causes this problem and input txt that doesn't.

我的第一个猜测是编码.我尝试使用BOM将输入文件的编码更改为UTF-8和UTF-8.但这失败了.

My first guess was encoding. I tried changing the encoding of the input file to UTF-8 and UTF-8 with BOM. But this failed.

我的第二个猜测是它实际上占用了过多的内存.但是我的计算机具有32 GB RAM的SSD.

My second guess was it used literally too much memory. But my computer has SSD with 32 GB RAM.

那么也许这段代码没有充分利用此RAM的容量?

So perhaps this code is not fully utilizing the capacity of this RAM?

我该如何解决?

我添加了那条线 打印你好世界"和 打印"hello world2"检查"hello world"之前的所有零件是否正确运行.

I added that line print 'hello world'and print 'hello world2'to check if all the parts before 'hello world' are run correctly.

我检查了代码打印的是"hello world",而不是"hello world2"

I checked the code prints 'hello world', but not 'hello world2'

所以,看来 wb.save(output_path)

So, it really seems likely that wb.save(output_path)

是造成问题的原因.

推荐答案

openpyxl具有用于读取和写入大文件的优化模式.wb = Workbook(write_only=True)将启用此功能.

openpyxl has optimised modes for reading and writing large files.wb = Workbook(write_only=True) will enable this.

我还建议您安装lxml以提高速度.这些都包含在文档中.

I'd also recommend that you install lxml for speed. This is all covered in the documentation.

这篇关于将txt转换为xlsx时出现MemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 12:22