I have a CSV data file with following data:
我有一个包含以下数据的CSV数据文件:
id,account_number,balance,date
1,ar161429,482.29,11/28/2007 15:54
2,ar182364,266.93,10/9/2007 15:54
3,ar106644,887.78,10/23/2007 15:54
I am trying to create an array for the date and account number, and then break the date down further into day, months and year. I was able to split the data based on "," and create an array for account and date but when I try to the split the date into month and day it is not working. Here's my code:
我正在尝试为日期和帐号创建一个数组,然后将日期进一步分解为日,月和年。我能够基于“,”分割数据,并为帐户和日期创建一个数组,但是当我尝试将日期分为月和日时,它不起作用。这是我的代码:
class Account
puts "Please Enter the name of account file in .txt format"
balance_file = gets.chomp
@number_lines = File.read(balance_file).scan(/\n/).count
@number_lines = @number_lines - 1
File.open(balance_file) do |aFile|
@@balance = []
@@balance_due = []
@@balance_due_day = []
@@balance_due_month =[]
@@balance_due_year = []
aFile.each_line do |line|
@@balance << line.split(",").values_at(2)
@@balance_due << line.split(",").values_at(3)
puts @@balance_due
end
i = 1
begin
@@balance_due_day[i] = @@balance_due[i].split("/").values_at(0)
@@balance_due_month[i] = @@balance_due[i].slice!(3,2)
i +=1
end while i<@number_lines
end
end
3 个解决方案
#1
1
Well, many things to tell:
好吧,有很多事要说:
First, slice!
ends with !
, so as most bang methods, it means that it changes the object itself instead of only return a new object. Example on console:
首先,切片!以!结尾,所以作为大多数爆炸方法,它意味着它改变对象本身而不是仅返回一个新对象。控制台上的示例:
> a = [1,2,3]
=> [1, 2, 3]
> a.slice(2,3)
=> [3]
> a
=> [1, 2, 3] # didn't modified a
> a.slice!(2,3)
=> [3]
> a
=> [1, 2] # modified a
Second, don't use while
to loop in a list. Simply use each (of course you can use it do
and end
if it is a multiline operation:
其次,不要使用while循环列表。只需使用每个(当然,如果是多线操作,您可以使用它并结束:
[1,2,3].each{|x| p x}
1
2
3
Third, the variables prefixed with double @
should not be what you are thinking it is. They are not used often. I suggest you to read about it.
第三,前缀为double @的变量不应该是你想象的那样。它们不经常使用。我建议你阅读一下。
To see what went wrong with your date extraction logic, it is a good idea to open the ruby terminal (command is irb
). Then type some lines, for example
要查看日期提取逻辑出了什么问题,打开ruby终端是个好主意(命令是irb)。然后键入一些行,例如
> a_date = a_date.split("/").values_at(0)
=> ["11"]
> a_date = a_date.slice!(3,2)
=> nil
I'm not correcting because I'm not 100% sure about what you wanted as output, but I think you can see the problem here now.
我没有纠正,因为我不是100%肯定你想要的输出,但我认为你现在可以看到这个问题。
Happy hacking
#2
1
You should use CSV and date:
你应该使用CSV和日期:
require 'csv'
require 'date'
date_and_account_number = []
CSV.foreach('data.csv', :headers => true) do |row|
date_and_account_number << [row[3],row[1]]
end
date_and_account_number.map! {|e| dt = DateTime.strptime(e[0],"%m/%d/%Y %H:%M"); [dt.day,dt.month,dt.year,e[1]] }
#=> [[28, 11, 2007, "ar161429"], [9, 10, 2007, "ar182364"], [23, 10, 2007, "ar106644"]]
#3
0
string = <<_
id,account_number,balance,date
1,ar161429,482.29,11/28/2007 15:54
2,ar182364,266.93,10/9/2007 15:54
3,ar106644,887.78,10/23/2007 15:54
_
string.sub(/^.*$/, "").scan(%r{^[^,]*,([^,]*),[^,]*,([^/]*)/([^/]*)/(\S*)\s+.*$})
# => [
# ["ar161429", "11", "28", "2007"],
# ["ar182364", "10", "9", "2007"],
# ["ar106644", "10", "23", "2007"]
# ]
#1
1
Well, many things to tell:
好吧,有很多事要说:
First, slice!
ends with !
, so as most bang methods, it means that it changes the object itself instead of only return a new object. Example on console:
首先,切片!以!结尾,所以作为大多数爆炸方法,它意味着它改变对象本身而不是仅返回一个新对象。控制台上的示例:
> a = [1,2,3]
=> [1, 2, 3]
> a.slice(2,3)
=> [3]
> a
=> [1, 2, 3] # didn't modified a
> a.slice!(2,3)
=> [3]
> a
=> [1, 2] # modified a
Second, don't use while
to loop in a list. Simply use each (of course you can use it do
and end
if it is a multiline operation:
其次,不要使用while循环列表。只需使用每个(当然,如果是多线操作,您可以使用它并结束:
[1,2,3].each{|x| p x}
1
2
3
Third, the variables prefixed with double @
should not be what you are thinking it is. They are not used often. I suggest you to read about it.
第三,前缀为double @的变量不应该是你想象的那样。它们不经常使用。我建议你阅读一下。
To see what went wrong with your date extraction logic, it is a good idea to open the ruby terminal (command is irb
). Then type some lines, for example
要查看日期提取逻辑出了什么问题,打开ruby终端是个好主意(命令是irb)。然后键入一些行,例如
> a_date = a_date.split("/").values_at(0)
=> ["11"]
> a_date = a_date.slice!(3,2)
=> nil
I'm not correcting because I'm not 100% sure about what you wanted as output, but I think you can see the problem here now.
我没有纠正,因为我不是100%肯定你想要的输出,但我认为你现在可以看到这个问题。
Happy hacking
#2
1
You should use CSV and date:
你应该使用CSV和日期:
require 'csv'
require 'date'
date_and_account_number = []
CSV.foreach('data.csv', :headers => true) do |row|
date_and_account_number << [row[3],row[1]]
end
date_and_account_number.map! {|e| dt = DateTime.strptime(e[0],"%m/%d/%Y %H:%M"); [dt.day,dt.month,dt.year,e[1]] }
#=> [[28, 11, 2007, "ar161429"], [9, 10, 2007, "ar182364"], [23, 10, 2007, "ar106644"]]
#3
0
string = <<_
id,account_number,balance,date
1,ar161429,482.29,11/28/2007 15:54
2,ar182364,266.93,10/9/2007 15:54
3,ar106644,887.78,10/23/2007 15:54
_
string.sub(/^.*$/, "").scan(%r{^[^,]*,([^,]*),[^,]*,([^/]*)/([^/]*)/(\S*)\s+.*$})
# => [
# ["ar161429", "11", "28", "2007"],
# ["ar182364", "10", "9", "2007"],
# ["ar106644", "10", "23", "2007"]
# ]