简体   繁体   English

用不同的字符串替换Ruby逗号

[英]Replace in Ruby commas by different strings

I have a string called "example", like this: 我有一个名为“ example”的字符串,如下所示:

192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812 192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812
192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807 192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807

and I would like to obtain this output: 我想获得此输出:

{"string1":"192.168.1.40","string2":"8.8.8.8",“string3":“12.34.45.56”,“string4”:408,“string5”:“-”,"string6":1812} {"string1":"192.168.1.128","string2":"192.168.101.222",“string3":“12.34.45.56”,“string4”:384,“string5”:“-”,"string6":1807} {“ string1”:“ 192.168.1.40”,“ string2”:“ 8.8.8.8”,“ string3”:“ 12.34.45.56”,“ string4”:408,“ string5”:“-”,“ string6”:1812 } {“ string1”:“ 192.168.1.128”,“ string2”:“ 192.168.101.222”,“ string3”:“ 12.34.45.56”,“ string4”:384,“ string5”:“-”,“ string6”: 1807}

I did this: 我这样做:

example = example.gsub("\n","}\n{\"string1\": \"")                         
example = example.insert(0, "{\"string1\": \"")                  
example = example.concat("}")                                    

and I obtained: 我获得了:

{"string1":"192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812} {"string1":"192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807} {“ string1”:“ 192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812} {” string1“:” 192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807}

but I don't know how can I do the others changes. 但我不知道该如何改变其他人。 Thanks!! 谢谢!!

Well, to get it as a ruby hash, which you can output as json or whatever you need: 好吧,以红宝石哈希的形式获取它,您可以将其输出为json或任何您需要的东西:

out = {}
your_input_data.split(",").each_with_index { |val, i| out["string#{i}"] = val  }

(but you would need to do this for each line: input.lines.each { |line| ... do the above here } - but I am not clear - do you want a list of maps?) (但是您需要为每一行执行以下操作:input.lines.each {| line | ...在这里做以上操作}-但我不清楚-您是否需要地图列表?)

I made the assumption that you didn't want values that were just numbers to be double-quoted. 我假设您不希望只是数字的值被双引号引起来。

DATA.each_line do |line|
    l = line.chomp.split(',').map.with_index do |v, i|
        v = v =~ /^\d+$/ ? v : "\"#{v}\""
        "\"string#{i+1}\":#{v}"
    end
    print "{", l.join(','), "}\n"
end

__END__
192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812
192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807

Result: 结果:

{"string1":"192.168.1.40","string2":"8.8.8.8","string3":"12.34.45.56","string4":408,"string5":"-","string6":1812}
{"string1":"192.168.1.128","string2":"192.168.101.222","string3":"12.34.45.56","string4":384,"string5":"-","string6":1807}

It seems from the code you wrote that you are looking for a single string as output rather than a more elaborate Ruby data structure or output to a printed stream. 从编写的代码看来,您正在寻找的是单个字符串作为输出,而不是更复杂的Ruby数据结构或输出到打印流。

This is working for me: 这为我工作:

example = '192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812
192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807'

result = example.split("\n").map do |line| 
  n = 0 
  line.split(',').map{|s| %Q|"string#{n+=1}":"#{s}"|}.join(',')
end.map{|c| "{#{c}}"}.join("\n")

puts result

{"string1":"192.168.1.40","string2":"8.8.8.8","string3":"12.34.45.56","string4":"408","string5":"-","string6":"1812"}
{"string1":" 192.168.1.128","string2":"192.168.101.222","string3":"12.34.45.56","string4":"384","string5":"-","string6":"1807"}

This splits into lines then splits each line into separate strings, then concatenates each string with its JSON key and finally reassembles with join first with commas and then with newline. 这将拆分为几行,然后将每一行拆分为单独的字符串,然后将每个字符串与其JSON密钥join起来,最后通过首先使用逗号和再换行的join重组。 If you'd rathet have lists than reassembled strings, just omit the respective join. 如果您有列表而不是重新组合的字符串,则只需省略相应的联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM