为什么从文件读取的数组包含字节

时间:2012-11-04 08:07:52

标签: arrays ruby file

我正在编写一个小系统来解析用逗号分隔的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

这是我访问返回数组中第一项时的反馈(s?):


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>

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

有谁能弄清楚这里有什么不对吗?

1 个答案:

答案 0 :(得分:1)

首先,显然不是您使用的代码,因为to_S之类的内容并不是红宝石的一部分,无论如何都会立即失败。

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

# $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使用正确的编码。