简体   繁体   English

使用Python 2解析超过255个字符的Excel工作表

[英]Parsing an Excel sheet with more than 255 chars using Python 2

I have an Excel spreadsheet with 3 rows that I am parsing using Python2, and exporting the data to a .txt file. 我有一个包含3行的Excel电子表格,该行使用Python2进行解析,并将数据导出到.txt文件。 The third row is much more than 255 characters, so when the parser hits the end of the 255 column length, it cuts out the rest of the data. 第三行远远超过255个字符,因此当解析器到达255列长度的末尾时,它将切出其余数据。 Is there anyway to deal with this? 反正有处理这个吗?

from openpyxl import load_workbook
outf = open('c:\\Python27\\scripts\\saba_parser\\index.txt', 'w')
outf2 = open('c:\\Python27\\scripts\\IMS\\cse_errors.txt', 'w')
wb = load_workbook('c:\\Python27\\scripts\\saba_parser\\Copy of Saba_Views_good.xlsx')
s1 = wb.worksheets[0]


for r in s1:
   name = str(r[0].value)
   if r[2].value == None:
      tail = "None"
   else:
      #info = r[2].value.split("FROM")[-1]
      info = str(r[2].value)
      head, sep, tail = info.partition("FROM")
outf.write(name+','+tail+"\n")
#outf.write(name+','+info+"\n")

Any help would be appreciated. 任何帮助,将不胜感激。

Maybe if you load the workbook as "read only" , you could handle the long columns. 也许如果将工作簿加载为“只读” ,则可以处理长列。

Apparently, if the file does not have the dimension properly set, this could lead to trouble 显然,如果文件的尺寸设置不正确,可能会导致麻烦

Read-only mode relies applications and libraries that created the file providing correct information about the worksheets, specifically the used part of it, known as the dimensions. 只读模式依赖于创建文件的应用程序和库,该文件提供有关工作表的正确信息,尤其是文件的已使用部分,称为维度。 Some applications set this incorrectly. 某些应用程序对此设置不正确。 You can check the apparent dimensions of a worksheet using ws.calculate_dimensions(). 您可以使用ws.calculate_dimensions()检查工作表的外观尺寸。 If this returns a range that you know is incorrect, say A1:A1 then simply resetting the max_row and max_column attributes should allow you to work with the file 如果返回的范围不正确,请说A1:A1,然后简单地重设max_row和max_column属性,即可使用该文件

Try using this patch to your code 尝试将此补丁用于您的代码

wb = load_workbook('c:\\Python27\\scripts\\saba_parser\\Copy of Saba_Views_good.xlsx',read_only=True)
s1 = wb.worksheets[0]
# Only use the next line if s1.calculate_dimensions() does not give you the expected result
# s1.max_row = s2.max_column = None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM