2012-11-04 35 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

相關問題