我正在尝试构建/更新字典.我有昵称作为temp_dict中的键,并寻找要添加的ID.
摘录自我的代码.我认为这足以让你看到我的错误.
d1 = {u'status': u'ok', u'count': 1, u'data': [{u'nickname': u'45sss', u'account_id': 553472}]}
temp_dict = {}
for key, value in d1.iteritems():
if "data" == key:
for dic2 in value:
x = dic2['nickname']
y = dic2['account_id']
temp_dict[x] = y;
Run Code Online (Sandbox Code Playgroud)
我的错误:
Traceback (most recent call last):
File "untitled.py", line 36, in <module>
get_PlayerIds_Names_WowpApi_TJ_() #Easy going. Some issues with case letters.
File "g:\Desktop\Programming\WOWP API\functions.py", line 44, in get_PlayerIds_Names_WowpApi_TJ_
check_missing_player_ids(basket)
File "g:\Desktop\Programming\WOWP API\functions.py", line 195, in check_missing_player_ids
temp_dict[x] = y;
TypeError: 'unicode' object does …Run Code Online (Sandbox Code Playgroud) 我试图从嵌套字典(从json加载)收集信息.我试图用for循环来做到这一点.我无法在字典中找到一个名为"players"的词典."玩家"包含带有玩家姓名及其ID的字典.我想提取那本字典.您可以在下面找到我的代码和数据样本.
我能够通过第一级迭代到字典,但我无法过滤掉更深层次.
我一直在寻找其他类似的问题,但他们正在解决字典迭代的不同问题.我无法将它们用于我的目的.我正在考虑使用data.keys()["players"]来提取我需要的信息,但我现在无法解决这个问题.
for key, value in dct.iteritems():
if value == "players":
for key, value in dct.iteritems():
print key, value
Run Code Online (Sandbox Code Playgroud)
我的数据样本:
{
"[CA1]": {
"team_tag": "[CA1]",
"team_name": "CzechAir",
"team_captain": "MatejCzE",
"players": {
"PeatCZ": "",
"MartyJameson": "",
"MidnightMaximus": "",
"vlak_in": "",
"DareD3v1l": "",
"Hugozhor78": ""
}
},
"[GWDYC]": {
"team_tag": "[GWDYC]",
"team_name": "Guys Who Dated Your Cousin",
"team_captain": "Teky1792",
"players": {
"wondy22": "",
"dzavo1221": "",
"Oremuss": "",
"Straker741": "",
"Vasek9266": ""
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想玩现在以JSON格式保存的数据.但我对R来说很新,并且对如何使用数据几乎没有任何线索.你可以在下面看到我设法实现的目标.但首先,我的代码:
library(rjson)
json_file <- "C:\\Users\\Saonkfas\\Desktop\\WOWPAPI\\wowpfinaljson.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
Run Code Online (Sandbox Code Playgroud)
我能够获得数据:
for (x in json_data){print (x)}
Run Code Online (Sandbox Code Playgroud)
虽然输出看起来很原始:
[[1]]
[[1]]$wins
[1] "118"
[[1]]$losses
[1] "40"
# And so on
Run Code Online (Sandbox Code Playgroud)
请注意,JSON有点嵌套.我可以使用Python创建表,但R看起来要复杂得多.
编辑:
我的JSON:
{
"play1": [
{
"wins": "118",
"losses": "40",
"max_killed": "7",
"battles": "158",
"plane_id": "4401",
"max_ground_object_destroyed": "3"
},
{
"wins": "100",
"losses": "58",
"max_killed": "7",
"battles": "158",
"plane_id": "2401",
"max_ground_object_destroyed": "3"
},
{
"wins": "120",
"losses": "38",
"max_killed": "7",
"battles": "158",
"plane_id": "2403",
"max_ground_object_destroyed": "3"
}
],
"play2": [
{
"wins": …Run Code Online (Sandbox Code Playgroud) 获得有向图的所有边对的最佳方法是什么.我只需要那些方向相反的边缘.我需要用它来比较关系的对称性.
我寻求以下结果(虽然我不确定是获得结果的最佳形式)输入:
[(a,b,{'weight':13}),
(b,a,{'weight':5}),
(b,c,{'weight':8}),
(c,b,{'weight':6}),
(c,d,{'weight':3}),
(c,e,{'weight':5})] #Last two should not appear in output because they do not have inverse edge.
Run Code Online (Sandbox Code Playgroud)
输出:
[set(a,b):[13,5],
set(b,c):[8,6]]
Run Code Online (Sandbox Code Playgroud)
这里的序列是导入的,因为它告诉了方向.
我该怎么看?
我想解析JSON.如果我在一行中编写JSON就可以了
json_input = '{ "rate_of_climbing": 18.4, "speed_factor": 520}'
Run Code Online (Sandbox Code Playgroud)
但是如果我有JSON格式化,那么解析器不起作用:
json_input = '{
"rate_of_climbing": 18.4,
"speed_factor": 520
}'
Run Code Online (Sandbox Code Playgroud)
如何获取JSON以读取格式化字符串?
我的完整代码:
import json
json_input = '{
"rate_of_climbing": 18.4,
"speed_factor": 520
}'
try:
decoded = json.loads(json_input)
print json.dumps(decoded, sort_keys=True, indent=4)
print "JSON parsing example: ", decoded['rate_of_climbing']
print "Complex JSON parsing example: ", decoded['speed_factor']
except (ValueError, KeyError, TypeError):
print "JSON format error"
Run Code Online (Sandbox Code Playgroud) 如何将utf-8字符写入csv文件?
我的数据和代码:
# -*- coding: utf-8 -*-
l1 = ["žžž", "???"]
l2 = ["žžž", "???"]
thelist = [l1, l2]
import csv
import codecs
with codecs.open('test', 'w', "utf-8-sig") as f:
writer = csv.writer(f)
for x in thelist:
print x
for mem in x:
writer.writerow(mem)
Run Code Online (Sandbox Code Playgroud)
错误消息:
Traceback (most recent call last):
File "2010rudeni priimti.py", line 263, in <module>
writer.writerow(mem)
File "C:\Python27\lib\codecs.py", line 691, in write
return self.writer.write(data)
File "C:\Python27\lib\codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
File "C:\Python27\lib\encodings\utf_8_sig.py", line 82, …Run Code Online (Sandbox Code Playgroud) 我现在有这个数据:
animal age count
dogs 1 49
2 134
3 147
4 154
cats 1 189
2 254
3 259
4 261
Run Code Online (Sandbox Code Playgroud)
我想将年龄列转换为每个年龄段的4个年龄段:
animal age1 age2 age3 age4
dogs 49 134 147 154
cats ....................
Run Code Online (Sandbox Code Playgroud)
我已经尝试过df.T和df.transpose(),但它们都返回了原始列.
我有不同球员的统计数据.我想知道如何操纵我的数据.我怎么能找到:
如何在三个矩阵的列中比较"胜利"的值?
library(jsonlite)
iris2 <- fromJSON("C:\\Users\\Saonkfas\\Desktop\\WOWPAPI\\wowpfinaljson.json")
print(iris2)
for (x in iris2){print (x)}
Run Code Online (Sandbox Code Playgroud)这是我打印后的内容:
wins losses max_killed battles plane_id max_ground_object_destroyed
1 118 40 7 158 4401 3
2 100 58 7 158 2401 3
3 120 38 7 158 2403 3
wins losses max_killed battles plane_id max_ground_object_destroyed
1 12 450 7 158 4401 3
2 150 8 7 158 2401 3
3 120 328 7 158 2403 3
wins losses max_killed battles plane_id max_ground_object_destroyed
1 158 40 7 158 …Run Code Online (Sandbox Code Playgroud) python ×6
json ×4
dictionary ×3
loops ×2
r ×2
combinations ×1
csv ×1
dataframe ×1
formatting ×1
iteration ×1
list ×1
matrix ×1
nested ×1
networkx ×1
pandas ×1
parsing ×1
permutation ×1
python-2.7 ×1
rjson ×1
transpose ×1
utf-8 ×1