找回密码
 加入
搜索
查看: 9608|回复: 17

[效率算法] [已解决]类似字典格式的怎么处理?

 火.. [复制链接]
发表于 2013-5-25 07:05:41 | 显示全部楼层 |阅读模式
本帖最后由 flyeblue 于 2013-5-30 03:49 编辑
{"p": {"out": 7, "incoming": 0}, "up": 30.358999967575073, "my_stale_r": {"orphan_stale": null, "stale": null, "dead_stale": null}, "attempts": 48049638107769001, "warnings": [], "block_value": 0.30771641, "dead_hash_rates": {}, "efficiency_if_miner_perfect": null, "shares": {"total": 0, "orphan": 0, "dead": 0}, "efficiency": null, "my_share_counts_in_last_hour": {"doa_stale_shares": 0, "stale_shares": 0, "orphan_stale_shares": 0, "shares": 0, "unstale_shares": 0}, "miner_hash_rates": {"099": 1164894834.2242064}, "last_hour": {"note": "DEPRECATED", "actual": 0, "rewarded": 0.0, "nonstale": 0.0}, "donation_proportion": 0.01, "attempts_to_share": 7448126081565}

上面这段数据,怎么才能在autoit里正确的处理?最好能够读取跟写入

看了大家的帮助,总算有了思路,现在基本实现了基础功能
Func _DictKeys($Dict)
        ;获取字典所有关键字
        Local $Keys[1]=[]
        Local $tempKeys
        $tempKeys=StringRegExp($Dict,'("\w+"):\h*(?:{.*?}|.+?(?=,\h*|}\s*$))',3)
        If IsArray($tempKeys) Then
;~                 ConsoleWrite('find'&@CRLF)
                $Keys=$tempKeys
        EndIf
        Return $Keys
EndFunc

Func _DictKeysCheck($dict,$CheckKey)
        ;检测指定的关键字是否存在字典中
        Local $Keys
        Local $temp
        $Keys=_DictKeys($dict)
        If IsArray($Keys) Then
                For $temp In $Keys
                        If $CheckKey=$temp Then
                                Return True
                        EndIf
                Next
        EndIf
        Return False        
EndFunc

Func _DictValue($dict,$DictKey)
        ;依据字典关键字获取字典值
        Local $tempValue,$Value=''
        Local $tempValueRegExp=':\h*((?:{.*?}|.+?(?=,\h*|}\s*$)))'
        If _DictKeysCheck($dict,$DictKey) Then
                $tempValueRegExp=$DictKey&$tempValueRegExp
                $tempValue=StringRegExp($dict,$tempValueRegExp,3)
                If IsArray($tempValue) Then
;~                         ConsoleWrite('find1'&@CRLF)
                        $Value=$tempValue[0]
                EndIf
        EndIf        
        Return $Value
        
EndFunc

Func _DictValueChang(ByRef $dict,$DictKey,$NewValue)
        ;改变字典项值,如果关键字不存在则不改变
        Local $tempValueRegExp=':\h*((?:{.*?}|.+?(?=,\h*|}\s*$)))'
;~         Local $tempDict
        If _DictKeysCheck($dict,$DictKey) Then
                $tempValueRegExp=$DictKey&$tempValueRegExp
;~                 ConsoleWrite($tempValueRegExp&@CRLF)
;~                 ConsoleWrite('find1'&@CRLF)
                $dict=StringRegExpReplace($dict,$tempValueRegExp,$DictKey&':'&$NewValue)
;~                 
;~         Else
;~                 $tempDict=$dict
        EndIf
;~         Return $tempDict
EndFunc        
Func _DictKeyDel(ByRef $dict,$DelKey)
        ;删除字典项,依据关键字
        Local $tempValueRegExp=':\h*((?:{.*?}|.+?(?=,\h*|}\s*$))),'
        If _DictKeysCheck($dict,$DelKey) Then
                $tempValueRegExp=$DelKey&$tempValueRegExp
                $dict=StringRegExpReplace($dict,$tempValueRegExp,'')
        EndIf
EndFunc

Func _DictAdd(ByRef $dict,$AddKey,$AddValue)
        ;添加字典项,如果字典关键字存在则不改变
        Local $tempRegExp='(}$)'
        Local $tempRegExpNew
        If Not _DictKeysCheck($dict,$AddKey) Then
                $tempRegExpNew=','&$AddKey&':'&$AddValue&'\1'
                $dict=StringRegExpReplace($dict,$tempRegExp,$tempRegExpNew)
        EndIf        
EndFunc
示例如下:
$stringDict='{"p": {"out": 7, "incoming": 0}, "up": 30.358999967575073, "my_stale_r": {"orphan_stale": null, "stale": null, "dead_stale": null}, "attempts": 48049638107769001, "warnings": [], "block_value": 0.30771641, "dead_hash_rates": {}, "efficiency_if_miner_perfect": null, "shares": {"total": 0, "orphan": 0, "dead": 0}, "efficiency": null, "my_share_counts_in_last_hour": {"doa_stale_shares": 0, "stale_shares": 0, "orphan_stale_shares": 0, "shares": 0, "unstale_shares": 0}, "miner_hash_rates": {"099": 1164894834.2242064}, "last_hour": {"note": "DEPRECATED", "actual": 0, "rewarded": 0.0, "nonstale": 0.0}, "donation_proportion": 0.01, "attempts_to_share": 7448126081565}'
;~ $stringDict='{"out": 7, "incoming": 0}
$temp=_DictKeys($stringDict)
If IsArray($temp) Then
        For $aa In $temp
                ConsoleWrite($aa&@CRLF)
        Next
Else
        ConsoleWrite('无'&@CRLF)
EndIf        
ConsoleWrite('************************'&@CRLF)
ConsoleWrite(_DictKeysCheck($stringDict,'"p"')&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"p"')&@CRLF)
ConsoleWrite('************************'&@CRLF)
ConsoleWrite(_DictKeysCheck($stringDict,'"incoming"')&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"incoming"')&@CRLF)
ConsoleWrite('************************'&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"out"')&@CRLF)
_DictValueChang($stringDict,'"out"','00000')
ConsoleWrite($stringDict&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"out"')&@CRLF)
ConsoleWrite('************************'&@CRLF)
_DictKeyDel($stringDict,'out')
ConsoleWrite($stringDict&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"out"')&@CRLF)
_DictKeyDel($stringDict,'"out"')
ConsoleWrite($stringDict&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"out"')&@CRLF)
ConsoleWrite('************************'&@CRLF)
ConsoleWrite(_DictValue($stringDict,'"up"')&@CRLF)
Local $t[1]=[]
If _DictValue($stringDict,'"up"')='' Then ConsoleWrite('空字符串'&@CRLF)
If _DictValue($stringDict,'"up"')=$t Then ConsoleWrite('空数组'&@CRLF)
_DictAdd($stringDict,'"up"',30.358999967575073)
ConsoleWrite(_DictValue($stringDict,'"up"')&@CRLF)
发表于 2013-5-25 09:14:29 | 显示全部楼层
什么叫正确处理?你想处理什么?问的问题都看不明白想干嘛
 楼主| 发表于 2013-5-25 10:44:09 | 显示全部楼层
本帖最后由 flyeblue 于 2013-5-25 10:45 编辑
什么叫正确处理?你想处理什么?问的问题都看不明白想干嘛
tryhi 发表于 2013-5-25 09:14

参考http://skyfen.iteye.com/blog/567571
需要类似的能读出这些的方法。应该类似xml文件的结构
发表于 2013-5-25 13:57:47 | 显示全部楼层
Python看不懂,帮不上!
 楼主| 发表于 2013-5-29 11:42:31 | 显示全部楼层
本帖最后由 flyeblue 于 2013-5-29 11:47 编辑

现在只能用处理字符串的方法处理,用起来好麻烦啊。。。
一种字典项,类似
 "p": {"out": 7, "incoming": 0} 
这样的 可以用匹配
[^{}]("\w+":\s+[^{},]+),
一种类似
"donation_proportion": 0.01
这样的,可以用
("\w+":\s{[^{}]+}),
不知道为啥
[^{}]("\w+":\s[^{},]+),|("\w+":\s{[^{}]+}),
会匹配空白字符
 楼主| 发表于 2013-5-29 12:10:49 | 显示全部楼层
((?<=[^{}])"\w+":\s+[^{},]+(?=,))|("\w+":\s{[^{}]+}(?=,))
也不行。郁闷啊
发表于 2013-5-29 12:35:46 | 显示全部楼层
回复 6# flyeblue
对这种字典结构不是很了解,也不明白楼主要提取什么,就上面这段字符串,楼主需要提取什么,请明示。
发表于 2013-5-29 12:47:23 | 显示全部楼层
本帖最后由 afan 于 2013-5-29 12:55 编辑
回复  flyeblue
对这种字典结构不是很了解,也不明白楼主要提取什么,就上面这段字符串,楼主需要提取什么 ...
xms77 发表于 2013-5-29 12:35



    同问~ 按常理估计应该是匹配到以下
"p": {"out": 7, "incoming": 0}
"up": 30.358999967575073
"my_stale_r": {"orphan_stale": null, "stale": null, "dead_stale": null}
"attempts": 48049638107769001
"warnings": []
"block_value": 0.30771641
"dead_hash_rates": {}
"efficiency_if_miner_perfect": null
"shares": {"total": 0, "orphan": 0, "dead": 0}
"efficiency": null
"my_share_counts_in_last_hour": {"doa_stale_shares": 0, "stale_shares": 0, "orphan_stale_shares": 0, "shares": 0, "unstale_shares": 0}
"miner_hash_rates": {"099": 1164894834.2242064}
"last_hour": {"note": "DEPRECATED", "actual": 0, "rewarded": 0.0, "nonstale": 0.0}
"donation_proportion": 0.01
"attempts_to_share": 7448126081565
顺便说一句 LZ,5#的代码段,2、4应该对调…

个人认为,有时候非得一次性匹配全部也不一定是好办法,不合理的组织会带来更多的麻烦,甚至会对效率有很大的影响…
发表于 2013-5-29 13:04:12 | 显示全部楼层
如果是我的理解的话
#include <Array.au3>
Local $Str = '{"p": {"out": 7, "incoming": 0}, "up": 30.358999967575073, "my_stale_r": {"orphan_stale": null, "stale": null, "dead_stale": null}, "attempts": 48049638107769001, "warnings": [], "block_value": 0.30771641, "dead_hash_rates": {}, "efficiency_if_miner_perfect": null, "shares": {"total": 0, "orphan": 0, "dead": 0}, "efficiency": null, "my_share_counts_in_last_hour": {"doa_stale_shares": 0, "stale_shares": 0, "orphan_stale_shares": 0, "shares": 0, "unstale_shares": 0}, "miner_hash_rates": {"099": 1164894834.2242064}, "last_hour": {"note": "DEPRECATED", "actual": 0, "rewarded": 0.0, "nonstale": 0.0}, "donation_proportion": 0.01, "attempts_to_share": 7448126081565}'
;MsgBox(0, '原字符串', $Str)
Local $aSR = StringRegExp($str, '"\w+":\h+(?:{.*?}|.+?(?=,\h+|}\s*$))', 3)
_ArrayDisplay($aSR, UBound($aSR))

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2013-5-29 13:25:27 | 显示全部楼层
(?>("\w+":\s{[^{}]*}(?=,)))|(?<=[^{}])"\w+":\s+[^{},]+(?=,?)
总算对了
发表于 2013-5-29 14:47:33 | 显示全部楼层
祝贺下lz
发表于 2013-5-29 16:04:49 | 显示全部楼层
学习了个~ ~
发表于 2013-5-29 17:11:07 | 显示全部楼层
josn数据格式我记得有udf
发表于 2013-5-29 17:32:34 | 显示全部楼层
回复 13# 298311657

josn是个好办法,有时不比正则差哦
 楼主| 发表于 2013-5-30 01:16:31 | 显示全部楼层
回复 13# 298311657


    没有在论坛搜索到相应的udf啊。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-12 01:01 , Processed in 0.085553 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表