【发布时间】:2016-12-03 05:13:27
【问题描述】:
现在我正致力于从 Ruby 中的文本文件中提取信息。 那么如何从以下文本文件中仅提取数字“0.6748984055823062”?
{
"sentiment_analysis": [
{
"positive": [
{
"sentiment": "Popular",
"topic": "games",
"score": 0.6748984055823062,
"original_text": "Popular games",
"original_length": 13,
"normalized_text": "Popular games",
"normalized_length": 13,
"offset": 0
},
{
"sentiment": "engaging",
"topic": "pop culture-inspired games",
"score": 0.6280145725181376,
"original_text": "engaging pop culture-inspired games",
"original_length": 35,
"normalized_text": "engaging pop culture-inspired games",
"normalized_length": 35,
"offset": 370
},
我尝试的是可以读取文件并通过以下代码逐行打印。
counter = 1
file = File.new("Code.org", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
file.close
我想将数字设置为一个变量,以便我可以处理它。
【问题讨论】:
-
我发现了如何读取文件并逐行打印。但是提取对我来说很难。
-
这看起来像是 JSON 文件的一部分。你试过
JSON.parse吗? -
哦,你可能是对的。文件格式看起来像 JSON。我会尝试 JSON.parse。
-
请阅读“minimal reproducible example”。询问时,代码和数据允许测试是很重要的。 JSON 示例在语法上不正确,迫使我们修复它只是为了帮助您。这会浪费我们的时间,所以请帮助我们。
标签: ruby text-files extract