小编pab*_*808的帖子

将Ruby数组解析为JSON

我有一些结果:

puts result
Run Code Online (Sandbox Code Playgroud)

看起来像这样的输出:

Allowed
20863963
1554906
Denied
3607325
0
Quarantined
156240 
0
Run Code Online (Sandbox Code Playgroud)

调试

p results
Run Code Online (Sandbox Code Playgroud)

产量

[["Allowed", 20863963, 1554906], ["Denied", 3607325, 0], ["Quarantined", 156194, 0]]
Run Code Online (Sandbox Code Playgroud)

标题是:

status,hits,page_views 
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为json.如果结果是标准的csv格式,那么它将是直截了当的,但如果结果格式如上所示,如何处理它?

预期输出与此类似:

[{"status":"Allowed","hits":"20863963","page_views":"1554906"},{"status":"Denied","hits":"3607325","page_views":"0"},{"status":"Quarantined","hits":"156240","page_views":"0"}]
Run Code Online (Sandbox Code Playgroud)

a = result.map{|s| {status: s[0], hits: s[1].to_i, page_views: s[2].to_i} }
puts a.to_json
Run Code Online (Sandbox Code Playgroud)

ruby json

9
推荐指数
2
解决办法
3万
查看次数

直接从子进程输出解析 json 输出

我无法弄清楚如何直接从子进程输出解析 json 输出。

代码片段

cmd = ['./mmdbinspect', '--db', '/usr/local/var/GeoIP/GeoLite2-City.mmdb', ip]
        # result returns json
        result = subprocess.run(cmd, stdout=subprocess.PIPE)
        result = json.loads(result)
        # parse & print
        print(result[0]['Lookup'])
        print(result[0]['Records'][0]['Record']['country']['iso_code'])
        print(result[0]['Records'][0]['Record']['country']['names']['en'])
Run Code Online (Sandbox Code Playgroud)

如果我将结果写入文件,然后执行 json.load ,它会按预期工作,但我想合并并跳过该步骤。

回溯错误

TypeError: the JSON object must be str, bytes or bytearray, not CompletedProcess
Run Code Online (Sandbox Code Playgroud)

python json subprocess

3
推荐指数
1
解决办法
3810
查看次数

File.readlines UTF-8中的无效字节序列(ArgumentError)

我正在处理一个文件,其中包含来自Web的数据,并在某些日志文件中遇到UTF-8(ArgumentError)错误中的无效字节序列.

a = File.readlines('log.csv').grep(/watch\?v=/).map do |s|
s = s.parse_csv;
{ timestamp: s[0], url: s[1], ip: s[3] }
end
puts a
Run Code Online (Sandbox Code Playgroud)

我想让这个解决方案正常运行.我见过有人在做

.encode!('UTF-8', 'UTF-8', :invalid => :replace)

但它似乎没有用File.readlines.

File.readlines('log.csv').encode!('UTF-8', 'UTF-8', :invalid => :replace).grep(/watch\?v=/)

':undefined方法`编码!' for#(NoMethodError)

什么是在文件读取过程中过滤/转换无效UTF-8字符最直接的方法?

尝试1

试过这个,但它失败了同样的无效字节序列错误.

IO.foreach('test.csv', 'r:bom|UTF-8').grep(/watch\?v=/).map do |s|
  # extract three columns: time stamp, url, ip
  s = s.parse_csv;
  { timestamp: s[0], url: s[1], ip: s[3] }
end
Run Code Online (Sandbox Code Playgroud)

这似乎对我有用.

a = File.readlines('log.csv', :encoding => 'ISO-8859-1').grep(/watch\?v=/).map do |s|
s …
Run Code Online (Sandbox Code Playgroud)

ruby

2
推荐指数
1
解决办法
7529
查看次数

不同的JSON响应 - {"count":"123"} vs {"count"=>"123"}

我有一些PHP代码查询MySQL数据库的计数.

通过浏览器查询时,我得到以下输出:

{"count":"123"}
Run Code Online (Sandbox Code Playgroud)

我还有一个Ruby脚本,它通过Net :: HTTP执行相同的PHP脚本,但输出不同:

{"count"=>"123"}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

//The URL
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
//Request URL
request = Net::HTTP::Get.new(uri.request_uri)
//Basic authentication
request.basic_auth("user1", "secret")
response = http.request(request)
//Response
response = JSON.parse(response.body)
puts results
//Value 'count'
count = JSON.parse(response.body)[0]
puts count
Run Code Online (Sandbox Code Playgroud)

谢谢.

php ruby json

1
推荐指数
1
解决办法
98
查看次数

插入sed

我从这开始:

{"Allowed","20863962"}
Run Code Online (Sandbox Code Playgroud)

并且想插入label:value:所以它看起来像这样:

{ label: "Allowed", value: "20863962" }
Run Code Online (Sandbox Code Playgroud)

我尝试使用sed,但是它替换了插入而不是插入.

echo "{"Allowed","20863962"}" | sed 's/A/label: /' | sed 's/[0-9]/value: /'
Run Code Online (Sandbox Code Playgroud)

产量

{label: llowed,value: 0863962}
Run Code Online (Sandbox Code Playgroud)

bash sed

1
推荐指数
1
解决办法
95
查看次数

标签 统计

json ×3

ruby ×3

bash ×1

php ×1

python ×1

sed ×1

subprocess ×1