相关的问题: 1. Error in converting txt to xlsx using python的MemoryError而转换到TXT XLSX
- Converting txt to xlsx while setting the cell property for number cells as number
我的代码是
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的区别。
我的第一个猜测是编码。我试着用BOM来改变输入文件的编码为UTF-8和UTF-8。但是这失败了。
我的第二个猜测是它使用的字面太多的内存。但是我的电脑有32 GB RAM的SSD。
所以也许这个代码没有充分利用这个RAM的容量?
我该如何解决这个问题?
编辑:我补充说,行 打印的“Hello World” 和 打印“你好world2” 检查,如果之前的“Hello World”的所有零件都正常运行。
我检查了代码打印 '世界你好',但不是 '你好world2'
所以,它真的很可能是 wb.save(output_path)
引起的问题。
有多大的文件? –
@PadraicCunningham One是19MB,另一个是32MB。两者都失败。 – user3123767
我认为可以安全地说,你有足够的内存,它总是保存你得到的错误? –