如何根据位置向Ruby中的文本文件中写入一些值 [英] How to write some value to a text file in ruby based on position

查看:210
本文介绍了如何根据位置向Ruby中的文本文件中写入一些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,这是一些独特的解决方案.我有一个文本文件,其中我必须根据某些位置替换某些值.这不是一个大文件,并且在任何给定时间总会在所有行中始终包含5行,且行数固定.但是我必须专门在某些位置替换soem文本.此外,我还可以将一些文本放在所需的位置,并每次用所需的值替换该文本.我不确定如何实施此解决方案.我已经给出了下面的示例.

I need some help is some unique solution. I have a text file in which I have to replace some value based on some position. This is not a big file and will always contain 5 lines with fixed number of length in all the lines at any given time. But I have to specficaly replace soem text in some position only. Further, i can also put in some text in required position and replace that text with required value every time. I am not sure how to implement this solution. I have given the example below.

Line 1 - 00000 This Is Me 12345 trying
Line 2 - 23456 This is line 2 987654
Line 3 - This is 345678 line 3 67890

考虑以上是我必须用来替换某些值的文件.像在第1行中一样,我必须将'00000'替换为'11111',在第2行中,我必须将'This'替换为'Line'或任何需要四位数的文本.该位置在文本文件中将始终保持不变.

Consider the above is the file I have to use to replace some values. Like in line 1, I have to replace '00000' with '11111' and in line 2, I have to replace 'This' with 'Line' or any require four digit text. The position will always remain the same in text file.

我有一个可行的解决方案,但这是根据位置读取文件而不是写入文件.有人可以根据位置同样提出麻烦的解决方案

I have a solution which works but this is for reading the file based on position and not for writing. Can someone please give a solution similarly for wrtiting aswell based on position

根据位置读取文件的解决方案:

def read_var file, line_nr, vbegin, vend 
    IO.readlines(file)[line_nr][vbegin..vend] 
end 

puts read_var("read_var_from_file.txt", 0, 1, 3) #line 0, beginning at 1, ending at 3 
#=>308 

puts read_var("read_var_from_file.txt", 1, 3, 6) 

#=>8522 

我也尝试过这种写作解决方案.这行得通,但我需要它根据位置或特定行中显示的文本来工作.

I have also tried this solution for writing. This works but I need it to work based on position or based on text present in the specific line.

探索了可写入文件的解决方案:

open(Dir.pwd + '/Files/Try.txt', 'w') { |f|
  f << "Four score\n"
  f << "and seven\n"
  f << "years ago\n"
}

推荐答案

我为您制作了工作样本anagraj.

I made you a working sample anagraj.

in_file = "in.txt"
out_file = "out.txt"

=begin
=>contents of file in.txt
00000 This Is Me 12345 trying 
23456 This is line 2 987654 
This is 345678 line 3 67890 
=end

def replace_in_file in_file, out_file, shreds
  File.open(out_file,"wb") do |file|
    File.read(in_file).each_line.with_index do |line, index| 
      shreds.each do |shred|
        if shred[:index]==index
          line[shred[:begin]..shred[:end]]=shred[:replace]
        end
      end
      file << line
    end
  end
end    


shreds = [
    {index:0, begin:0, end:4, replace:"11111"},
    {index:1, begin:6, end:9, replace:"Line"}
]

replace_in_file in_file, out_file, shreds

=begin
=>contents of file out.txt
11111 This Is Me 12345 trying 
23456 Line is line 2 987654 
This is 345678 line 3 67890 
=end

这篇关于如何根据位置向Ruby中的文本文件中写入一些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆