Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Ruby中的文本文件中提取选定数据_Ruby_Text Files_Extract - Fatal编程技术网

从Ruby中的文本文件中提取选定数据

从Ruby中的文本文件中提取选定数据,ruby,text-files,extract,Ruby,Text Files,Extract,现在我正在用Ruby从文本文件中提取信息。 那么,如何从以下文本文件中仅提取数字“0.6748984055823062” { "sentiment_analysis": [ { "positive": [ { "sentiment": "Popular", "topic": "games", "score": 0.6748984055823062, "original_text

现在我正在用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字符串。在这种情况下,您可以解析它并执行以下操作:

require 'json'

file = File.read('Code.org')
data_hash = JSON.parse(file)

score = data_hash['score']

这里有一个脚本,它只提取你想要的分数

要记住两件事:

你要找的分数可能不是第一个 数据是数组和散列的混合体
我学会了如何读取文件并逐行打印。但是提取对我来说很难。这看起来像是JSON文件的一部分。你试过了吗?哦,你可能是对的。文件格式类似于JSON。我将尝试JSON.parse。请阅读。询问时,代码和数据允许测试是很重要的。JSON示例的语法不正确,迫使我们修复它只是为了帮助您。这浪费了我们的时间,所以请帮助我们帮助你。你能再看一下文件吗?文本是嵌套的,因此看起来我无法使用您建议的代码。您要求的数字没有出现在您提供的示例中,但应该可以导航到它或使用[]迭代所有可能的匹配项。Ruby不会对字符串文本使用反勾号。可以接受单引号或双引号,并将其用作require语句的参数。backticks用于在子shell中执行命令并返回STDOUT。此外,data_hash['score']不会返回OP想要的内容,而是返回'nil'。我建议你试着运行你的代码。@theTinMan是的,那完全是我输入的错误。谢谢你修好它
json_string = %q${
  "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
        }
      ]
    }
  ]
}
$

require 'json'
json = JSON.parse(json_string)

puts json["sentiment_analysis"].first["positive"].first["score"]
#=> 0.6748984055823062