Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
嘿。如何获取文件中的总行数(不想使用循环)。我正在阅读 CSV 文件。
示例 1
CSV.open('clients.csv', 'r')
示例 2
FasterCSV.foreach('clients.csv')
谢谢。
你的文件有多大?
此选项将整个文件加载到内存中,因此如果存在大小/内存问题,它可能无法正常工作。
numrows = FasterCSV.read('clients.csv').size
此选项使用 Ruby 的内置 CSV 模块,如您所知,它很慢,但它确实有效。它还将整个文件加载到内存中:
numrows = CSV.readlines('clients.csv').size
FasterCSV.read和CSV.readlines都返回数组数组,因此您可以对结果使用任何您想要的数组魔法。