从文本文件中读取一行,将该行的字符串版本拆分为两部分

时间:2016-02-07 22:04:26

标签: ruby string split slice

新手学习Ruby。

我正在尝试获取一个txt文件,并在每一行中取前三个字符并将它们指定为键,其余字符串作为键的值。

f = File.open("textfile.txt", "r")
finalHash = {"Key" => "Data"}
lineString = ""


while f.gets != nil do
  lineString = f.gets
  part1 = lineString.slice(0, 2)
  part2 = lineString.slice(3, lineString.length)
  finalHash[:part1] = part2
end

puts finalHash

感谢任何建议!

5 个答案:

答案 0 :(得分:1)

slice的第二个参数是长度,而不是结束索引,所以改变:

part1 = lineString.slice(0, 2)

为:

part1 = lineString.slice(0, 3)
  

如果传递了一个起始索引和一个长度,则返回一个包含的子字符串   从索引开始的长度字符

此外,您不需要第二个参数(但这不是错误):

part2 = lineString.slice(3, lineString.length)

这就够了:

part2 = lineString.slice(3)

答案 1 :(得分:1)

让我们先创建一个文件:

text = <<_
Now is the
time for all
good Rubiests
to come to the
aid of their
bowling team.
_

FName = 'temp'

File.write(FName, text)
  #=> 80

现在一次读取一行文件并构造所需的散列:

File.foreach(FName).with_object({}) do |line, h|
  h[line.slice!(0,3)] = line.chomp
end
  #=> {"Now"=>" is the", "tim"=>"e for all", "goo"=>"d Rubiests",
  #    "to "=>"come to the", "aid"=>" of their", "bow"=>"ling team."} 

阅读完第一行后,

h = { "Now"=>" is the" }

line = "time for all\n"

a = line.chomp
  #=> "time for all"
b = a.slice!(0,3)
  #=> "tim"
a #=> "e for all" 
h[b] = a
  #=> "e for all" 
h #=> {"Now"=>" is the", "tim"=>"e for all"}

如果一行包含少于三个字符,则不会给出方向。这可能需要考虑。

答案 2 :(得分:0)

f = File.open("textfile.txt")
hsh = {}
loop do
  x = f.gets
  break unless x
  hsh[x[0..2]] = x[3..-1]
end

使用慢慢蚕食文件的方法

barGraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(context));

答案 3 :(得分:0)

你走了:

示例数据:

[zatcsv]$ cat foo.txt 
TOK UPDATE DATE SHOT TIME AUXHEAT PHASE STATE PGASA PGASZ BGASA BGASZ BGASA2 BGASZ2 PIMPA
PIMPZ PELLET RGEO RMAG AMIN SEPLIM XPLIM KAPPA DELTA INDENT AREA VOL CONFIG IGRADB WALMAT DIVMAT LIMMAT EVAP 
BT IP VSURF Q95 BEPMHD BETMHD BEPDIA NEL DNELDT ZEFF PRAD POHM ENBI PINJ BSOURCE PINJ2 BSOURCE2 COCTR PNBI ECHFREQ 
ECHMODE ECHLOC PECH ICFREQ ICSCHEME ICANTEN PICRH LHFREQ LHNPAR PLH IBWFREQ PIBW TE0 TI0 WFANI WFICRH MEFF ISEQ WTH WTOT
JET 20031201 20001006 53521 1.000E+01 NBIC HSELM TRANS 2.000E+00 1.000E+00 2 1 0 0 1.658E+01 8.152E+00 NONE 2.888E+00 
HEEH OIJ OIJJ 3.047E+00 9.807E-01 2.924E-02 7.304E-02 1.572E+00 1.781E-01 0.000E+00 4.572E+00 8.161E+01 LSN 1 IN/
2.000E+06 1.013E-01 6.001E+00 1.053E+00 9.252E-01 1.128E+00 3.106E+19 3.106E+19 6.612E+00 4.515E+06 5.122E+04 1.000E+05 1.466E+07
771706 0.000E+00 652114 1.000E+00 1.420E+07 -9.999E-09 NONE NONE 0.000E+00 5.100E+07 HMIN MONOPOLE 4.027E+06 3.700E+09 1.840E+00
2.000E+06 -9.999E-09 0.000E+00 9.295E+03 1.373E+04 6.913E-01 7.319E+05 2.000E+00 NONE 3.715E+06 5.381E+06 1.282E+06 1.297E+07 1.210E+07

这样的事情会为你做到:

[za csv]$cat text_to_hash.rb
#!/usr/bin/env ruby

file_dir = "/dir/to_folder/foo.txt"

 thehash = Hash.new

line = File.read(file_dir).each_line do |line|

  thehash[ key = line.slice(0..2)] = val = line.slice(3..-1)

  thehash.each { |k , val| puts " Key: #{key} Value:  #{val}"} 

end

输出:

[za csv]$ ./text_to_hash.rb
 Key: TOK Value:   UPDATE DATE SHOT TIME AUXHEAT PHASE STATE PGASA PGASZ BGASA BGASZ BGASA2 BGASZ2 PIMPA
 Key: PIM Value:  PZ PELLET RGEO RMAG AMIN SEPLIM XPLIM KAPPA DELTA INDENT AREA VOL CONFIG IGRADB WALMAT DIVMAT LIMMAT EVAP
 Key: ECH Value:  MODE ECHLOC PECH ICFREQ ICSCHEME ICANTEN PICRH LHFREQ LHNPAR PLH IBWFREQ PIBW TE0 TI0 WFANI WFICRH MEFF ISEQ WTH WTOT
 Key: JET Value:   20031201 20001006 53521 1.000E+01 NBIC HSELM TRANS 2.000E+00 1.000E+00 2 1 0 0 1.658E+01 8.152E+00 NONE 2.888E+00 
 Key: HEE Value:  H OIJ OIJJ 3.047E+00 9.807E-01 2.924E-02 7.304E-02 1.572E+00 1.781E-01 0.000E+00 4.572E+00 8.161E+01 LSN 1 IN/
 Key: 2.0 Value:  00E+06 1.013E-01 6.001E+00 1.053E+00 9.252E-01 1.128E+00 3.106E+19 3.106E+19 6.612E+00 4.515E+06 5.122E+04 1.000E+05 1.466E+07
 Key: 771 Value:  706 0.000E+00 652114 1.000E+00 1.420E+07 -9.999E-09 NONE NONE 0.000E+00 5.100E+07 HMIN MONOPOLE 4.027E+06 3.700E+09 1.840E+00
 Key: 2.0 Value:  00E+06 -9.999E-09 0.000E+00 9.295E+03 1.373E+04 6.913E-01 7.319E+05 2.000E+00 NONE 3.715E+06 5.381E+06 1.282E+06 1.297E+07 1.210E+07
 Key: 4.4 Value:  45E-01 2.194E-01

答案 4 :(得分:0)

借用@ Cary的示例文件......

text = <<_
Now is the
time for all
good Rubiests
to come to the
aid of their
bowling team.
_

FName = 'temp'

File.write(FName, text)

现在该文件存在。将其转换为二维数组。这个数组很容易转换为哈希

File.foreach(FName).map{|x| [x.slice!(0,3), x]}.to_h

=> {"Now"=>" is the\n", "tim"=>"e for all\n", "goo"=>"d Rubiests\n", "to "=>"come to the\n", "aid"=>" of their\n", "bow"=>"ling team.\n"}