问题描述
我有一个名为 ama.txt 的文件,文件内容是这样的:
I have a file named ama.txt and the content of the file is like this:
name age hobby sex
amit 45 music male
sumit 35 cricket female
现在我可以像这样打开文件:
Now I can open the file like this:
File.open("ama.txt").each do
end
如何修改文件的hobby
栏?
推荐答案
这种事情的一般模式大致如下:
The general pattern for something like this is roughly as follows:
- 打开原始文件进行读取,打开一个新文件进行写入.逐行阅读原文.
- 跳过任何不相关的行(例如,您想跳过标题行和空格)
- 根据需要处理各个行.在这里你可能想要类似
cols = line.split(/\s+/)
的东西,然后你想编辑cols[2]
,虽然我不知道更改适合某种模式或什么. - 完成编辑后,将编辑后的行打印到新文件.
- 完成读取原始文件并将更改的行写入新文件后,关闭这两个文件.
- 将原始文件的名称更改为original_name"+.bak",并将新文件的名称更改为original_name".
- 完成.
- Open the original file for reading and a new file for writing. Read the original line by line.
- Skip any lines that aren't relevant (you want to skip your header line and the blanks, for example)
- Deal with the individual lines however you need. Here you probably want something like
cols = line.split(/\s+/)
and then you want to editcols[2]
, though I don't know if the changes fit a pattern or what. - Once you have the edits made, print the edited line out to the new file.
- Once you're done with reading the original and writing the changed lines to the new file, close both files.
- Change the original's name to something like 'original_name' + '.bak' and the new file's name to 'original_name'.
- Done.
Ruby 与大多数脚本语言一样,有很多内置方法可以使其中一些步骤更快进行,但这是基本模式.Dmitry 可能是正确的,CSV 阅读器会在这里帮助您,但我看不出您的数据格式(或格式不正确)如何,所以我什至不打算进入.
Ruby, like most scripting languages, has a lot of built-in ways to make some of these steps go quicker, but that's the basic pattern. Dmitry is potentially right that a CSV reader would help you here, but I can't see how well-formatted (or not) your data is, so I'm not even going to get into that.
最后,不要粗鲁,但您似乎对 Ruby 不是特别了解.为什么需要 Ruby?你知道另一种脚本语言吗?你对 Ruby 了解多少?一般来说,这里的人会帮你做事情,而不是简单地为你写代码.
Finally, and not to be rude, but it doesn't seem that you know Ruby especially well. Why is Ruby a requirement? Do you know another scripting language? How well do you know Ruby? In general, people here will help you with things, but not simply write code for you.
回答 Amit 关于第 4 步的问题:如果您打开了一个新文件用于写入,您将拥有一个文件句柄 - 程序中指向打开文件的变量.您可以使用文件句柄作为 puts
的接收器来写入该文件.这起初看起来很奇怪,因为 puts
通常看起来像一个函数,但它是 Ruby 中的方法调用.在 irb
或一个简短的 Ruby 脚本中试试这个:
To answer Amit's question about step 4: If you have a new file open for writing, you will have a filehandle - a variable in your program pointing to the open file. You can write to that file by using the filehandle as a receiver for puts
. This looks weird at first, since puts
looks like a function normally, but it is a method call in Ruby. Try this in irb
or a short Ruby script:
fh = File.open('test.txt', 'w')
fh.puts "hello, world"
fh.close
另一个简短的例子:
#!/usr/bin/env ruby
out_file = File.open('output.txt', 'w')
File.open('input.txt', 'r').each do |line|
out_file.print line.sub('foo', 'bar')
end
out_file.close
# no need to close the input file since the block version closes it for us
# automagically
# Maybe better to put the whole thing in a double block, though you may
# find this harder to follow at first
File.open('output.txt', 'w') do |out_file|
File.open('input.txt', 'r').each do |line|
out_file.print line.sub('foo', 'bar')
end
end
这篇关于如何在ruby中修改文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!