简体   繁体   English

使用file.readlines.size获取Ruby中xlsx文件的总行数,但返回错误的行数

[英]Using file.readlines.size to get the total number of lines of xlsx files in Ruby, but it returned the wrong number of rows

In ruby, I tried to use file.readlines.size to get the total number of rows in xlsx files, but there are 1200 rows in my files, file.readlines.size return me 322 insdead of 1200. I want to know why it's wrong and what's is the correct way to get the number of rows in a xlsx file. 在ruby中,我尝试使用file.readlines.size来获取xlsx文件中的总行数,但是我的文件中有1200行,file.readlines.size返回322的insdead为1200。我想知道为什么会这样错误,并且获取xlsx文件中的行数的正确方法是什么。 Thank you 谢谢

It returns the wrong size because file.readlines looks for line breaks to separate lines from one another. 它返回错误的大小,因为file.readlines查找换行符以使行彼此分开。 This only works with text files and xlsx is either binary or xml-based file. 这仅适用于文本文件,而xlsx是二进制文件或基于xml的文件。 You need to parse the file using a specialized parser to find out how many spreadsheet rows are in it. 您需要使用专门的解析器来解析文件,以找出其中有多少个电子表格行。 One gem which does it is Roo . 做到这一点的一颗宝石是Roo

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

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