-2

我正在编写一个小系统,它解析用逗号分隔的 txt 中的数据行,所以基本来说,我将文件行读入一个数组,然后在数组上使用 .each 并用“'”分割所有内容,然后推送将它放入作为由文件创建的数据库返回的保存数组中,我做了两个,第一个工作正常,但它的数据用关键字逐行存储,这个工作正常,访问和返回都很好。

我正在使用包含这样的文本数据的文件


476,TACKLE,40,25,30,0,0,1,A3F,move description string with, punctuation and t's
477,ANOTHERATTACK,BLAHBLAHBLAH,1,2,3,4

这将是一种正确的数据解析,嗯

所以我去:


$fs  = File_SYstem.new
@path = Dir.getwd.to_S + "/desktop/file.txt"
@data_lines = $fs.file_read_lines(@path)
@data = []
@data_lines.each do |line|
  @data >> line.split(',')
end
return @data
#this would make an array of the lines, each line being an array of its elements, right?

@data = The_Code_Above_In_A_Class.new(@path)
=>@data

@data[0]
=>"354,FISSURE,10,40,50,blah blah blah, the second half of the text."

#hmmmm

@data[0][0]
=>"354"

所以它似乎工作正常,但有时,开头的数字以字节的形式返回:O

例如:

@data.each do |line|
  puts line[1].to_S #return second element which is name of move
end

这将打印一个预期名称的列表,很好而且花花公子,但随后我得到了我没有要求的剩余数据,以无法识别的模式返回到它下面。

也许我可以做到这一点?

array = [1,2,3]
array = [array,array,array]
array[2][0] = "Hello!"
array.each do |item|
  puts item[2]
end
 =>"3"
"3"
"Hello!"
=>:

在我看来,这应该可行,因为我已经在其他地方成功地使用了这种风格的紧密变体。

现在这是一个真正的 580 行文件的示例:


1,MEGAHORN,Megahorn,000,120,BUG,Physical,85,10,0,00,0,abef,Cool,"Using its tough and  impressive horn, the user rams into the target with no letup."
2,ATTACKORDER,Attack Order,000,90,BUG,Physical,100,15,0,00,0,befh,Smart,The user calls out its underlings to pummel the target. Critical hits land more easily.
3,BUGBUZZ,Bug Buzz,046,90,BUG,Special,100,10,10,00,0,bek,Cute,The user vibrates its wings to generate a damaging sound wave. It may also lower the target's Sp. Def stat.

现在这是我用来加载它的类:

class Move_Data_Extracter
  def initialize(path)
        load $path.to_s + "/source/string_helper.rb"
    #load "/mnt/sdcard/pokemon/system/source/string_helper.rb"
        @path = path.to_s
    @file_lines = $file_system.file_read_lines(@path.to_s)
    $movedata = []
    @file_lines.each do |line|
      $movedata << line.split(",")
    end
  end
  def get_move_id(move_name)
    $movedata.each do |move|
      if move[1].upcase.to_s == move_name.upcase.to_s
            return move[0].to_i
      else
        return "Move Doesnt Exist In The System!"
      end
    end
  end
 end

这是我访问返回数组中的第一项时得到的反馈:


irb(main):002:0> $movedata[0]
=> ["\xEF\xBB\xBF1", "MEGAHORN", "Megahorn", "000", "120", "BUG", "Physical", "8
5", "10", "0", "00", "0", "abef", "Cool", "\"Using its tough and impressive horn
", " the user rams into the target with no letup.\"\n"]
irb(main):003:0> $movedata[0][0]
=> "\xEF\xBB\xBF1"
irb(main):004:0>

这次访问工作正常,但第一个元素是字节,我尝试的每个方法都出错了。

谁能弄清楚这里有什么问题?

4

1 回答 1

1

首先,这显然不是您正在使用的代码,因为to_S它不是 ruby​​ 的一部分,并且无论如何都会立即失败。

让我们稍微清理一下代码:

# $fs  = File_SYstem.new # this is just not needed
path = File.expand_path "/desktop/file.txt" # instance variables *only* within explicit objects
data_lines = File.read( path ).split ","

我不知道你写的其余部分的真正含义。

这输出:

# => ["476", "TACKLE", "40", "25", "30", "0", "0", "1", "A3F", "move description string with", " punctuation and t's\n477", "ANOTHERATTACK", "BLAHBLAHBLAH", "1", "2", "3", "4"]

这段代码 - 它是什么?

array = [1,2,3]
array = [array,array,array] # pure craziness!
array[2][0] = "Hello!"
array.each do |item|
  puts item[2]
end
 =>"3"
"3"
"Hello!"
=>:

至于为什么要取回字节,这是因为该文件(可能)被编码为 UTF-8。尝试File.read( path, "r:UTF-8")让 Ruby 使用正确的编码。

于 2012-11-04T08:36:56.893 回答