2012-11-04 18 views
-2

我正在写一个小系统来分析txt中由逗号分隔的数据, 所以基本就是这样,我将文件行读入数组,然后使用。每个都在数组上,然后将所有内容按“'”分开,然后将它按入数组中,然后将数据库作为数据库返回,我做了两个,第一个工作正常,但它的数据按关键字逐行存储,这个工作正常,访问并返回所有好的。为什么我从文件中读取的数组包含字节

我使用的是含有这样


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

首先,这显然不是你使用的代码,因为像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使用正确的编码。

+0

谢谢你是一个关键的延迟你知道我的意思,谢谢你帮助字节虽然 –

+0

@JakeSlone阅读常见问题 - 粗鲁不容忍。不,我不知道你的意思,如果你这样做,那么你需要再次考虑你的代码。硬。我标记过你,因为使用“延缓”这个词令人厌恶,你应该更加认真思考。 – iain

相关问题