开发者

How to prevent quote signs in File.readlines

开发者 https://www.devze.com 2023-03-07 13:45 出处:网络
For example 开发者_开发百科I have file file_name with such content: Just some text, nothing more

For example 开发者_开发百科I have file file_name with such content:

Just some text,
nothing more

Then I run kind of this code:

lines = File.open(file_name, "r").readlines
# do something do with lines
File.open(file_name, "w").write(lines)

I'll get this text

"Just some text,"
"nothing more"

How to prevent " sign here? I want text without quotes. Thanks


If you are using ruby 1.9.2, Array#to_s works like Array#inspect. Try this instead (some style tweaks thrown in):

lines = File.readlines(file_name)
File.open(file_name, 'w') { |f| f.write(lines.join) }


If you are only concerned with the quotes enclosing each line

okay, let's try that again

lines.gsub(/^"|"$/, '')

should work

0

精彩评论

暂无评论...
验证码 换一张
取 消