[已解决]类似字典格式的怎么处理?
本帖最后由 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里正确的处理?最好能够读取跟写入
看了大家的帮助,总算有了思路,现在基本实现了基础功能{:face (239):}
Func _DictKeys($Dict)
;获取字典所有关键字
Local $Keys=[]
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
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=[]
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)
什么叫正确处理?你想处理什么?问的问题都看不明白想干嘛 本帖最后由 flyeblue 于 2013-5-25 10:45 编辑
什么叫正确处理?你想处理什么?问的问题都看不明白想干嘛
tryhi 发表于 2013-5-25 09:14 http://www.autoitx.com/images/common/back.gif
参考http://skyfen.iteye.com/blog/567571
需要类似的能读出这些的方法。应该类似xml文件的结构 Python看不懂,帮不上! 本帖最后由 flyeblue 于 2013-5-29 11:47 编辑
现在只能用处理字符串的方法处理,用起来好麻烦啊。。。
一种字典项,类似 "p": {"out": 7, "incoming": 0} 这样的 可以用匹配[^{}]("\w+":\s+[^{},]+),一种类似"donation_proportion": 0.01这样的,可以用("\w+":\s{[^{}]+}),不知道为啥[^{}]("\w+":\s[^{},]+),|("\w+":\s{[^{}]+}),会匹配空白字符{:face (319):} ((?<=[^{}])"\w+":\s+[^{},]+(?=,))|("\w+":\s{[^{}]+}(?=,))也不行。郁闷啊 回复 6# flyeblue
对这种字典结构不是很了解,也不明白楼主要提取什么,就上面这段字符串,楼主需要提取什么,请明示。 本帖最后由 afan 于 2013-5-29 12:55 编辑
回复flyeblue
对这种字典结构不是很了解,也不明白楼主要提取什么,就上面这段字符串,楼主需要提取什么 ...
xms77 发表于 2013-5-29 12:35 http://www.autoitx.com/images/common/back.gif
同问~ 按常理估计应该是匹配到以下"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应该对调…
个人认为,有时候非得一次性匹配全部也不一定是好办法,不合理的组织会带来更多的麻烦,甚至会对效率有很大的影响… 如果是我的理解的话
#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)) (?>("\w+":\s{[^{}]*}(?=,)))|(?<=[^{}])"\w+":\s+[^{},]+(?=,?)总算对了 祝贺下lz{:face (125):} 学习了个~ ~ josn数据格式我记得有udf 回复 13# 298311657
josn是个好办法,有时不比正则差哦 回复 13# 298311657
没有在论坛搜索到相应的udf啊。
页:
[1]
2