我有以下文本文件。
bob jones,19,moore reef,yes,no
jenny smith, 21,reef park,yes,yes
any body, 12,somewhere,no,no
我希望每一行都在一个单独的数组中,或者我希望每个数据片段都有单独的数组(例如.name_array,age_array,destination_array等)。
我该怎么做?
答案 0 :(得分:5)
使用标准库中的CSV解析器加载它:
require 'csv'
a = CSV.open('x.csv').map { |r| r.map(&:strip) }
会给你一个a
这样的:
[
[ "bob jones", "19", "moore reef", "yes", "no" ],
[ "jenny smith", "21", "reef park", "yes", "yes" ],
[ "any body", "12", "somewhere", "no", "no" ]
]
然后,您可以使用b = a.transpose
翻转行和列,以便在b
中获取此内容:
[
[ "bob jones", "jenny smith", "any body" ],
[ "19", "21", "12" ],
[ "moore reef", "reef park", "somewhere" ],
[ "yes", "yes", "no" ],
[ "no", "yes", "no" ]
]
然后从那里你可以根据需要拉出各个数组:
names, ages, destinations, flags1, flags2 = *b
我不得不猜测flags1
和flags2
名称。
答案 1 :(得分:1)
将文件拆分为readlines
行,然后每行使用逗号String#split
。然后,如果你喜欢(为你的“第二种方式”)转置。
给定文件people.txt
bob jones,19,moore reef,yes,no
jenny smith, 21,reef park,yes,yes
any body, 12,somewhere,no,no
然后这样的文件行被删除:
people = IO.readlines("people.txt")
p people
这给出了
["bob jones,19,moore reef,yes,no\n",
"jenny smith, 21,reef park,yes,yes\n",
"any body, 12,somewhere,no,no"]
此时你还有一些额外的换行符,但是当你逐行处理时,它们会消失,添加String#chomp
:
result = []
people = IO.readlines("people.txt")
people.each {|line| result << line.chomp.split(",")}
p result
这给出了
[["bob jones", "19", "moore reef", "yes", "no"],
["jenny smith", " 21", "reef park", "yes", "yes"],
["any body", " 12", "somewhere", "no", "no"]]
问题的第二部分是如何为每列获取切片。最简单的方法是使用transpose
。像这样:
result = []
people = IO.readlines("people.txt")
people.each {|line| result << line.chomp.split(",")}
p result.transpose
这给出了
[["bob jones", "jenny smith", "any body"],
["19", " 21", " 12"],
["moore reef", "reef park", "somewhere"],
["yes", "yes", "no"],
["no", "yes", "no"]]
更新我必须编辑我的答案才能加入chomps!另外,您也可以添加一些字符串修剪。我会让你看那个。 : - )
答案 2 :(得分:0)
我这样做了;
f1 = File.open(filename,"r")
f1.each do | line|
i += 1
data = line.split(',')
#store data into arrays
passenger_name[i] = data[0] #passenger's name
passenger_age[i] = data[1]
passenger_destination[i] = data[2]
passenger_meal[i] = data[3]
passenger_dive[i] = data[4]
end
它将每个名称,年龄,目的地等存储在不同的数组中,从而更好地解决了我的问题。感谢您的所有回复。