我正在从一个JSON文件“new.JSON”中获取一些数据,我想过滤一些数据并将其存储到一个新的JSON文件中。下面是我的代码:
import json
with open('new.json') as infile:
data = json.load(infile)
for item in data:
iden = item.get["id"]
a = item.get["a"]
b = item.get["b"]
c = item.get["c"]
if c == 'XYZ' or "XYZ" in data["text"]:
filename = 'abc.json'
try:
outfile = open(filename,'ab')
except:
outfile = open(filename,'wb')
obj_json={}
obj_json["ID"] = iden
obj_json["VAL_A"] = a
obj_json["VAL_B"] = b
File "rtfav.py", line 3, in <module>
data = json.load(infile)
File "/usr/lib64/python2.7/json/__init__.py", line 278, in load
**kw)
File "/usr/lib64/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 88 column 2 - line 50607 column 2 (char 3077 - 1868399)
{
"contributors": null,
"truncated": false,
"text": "@HomeShop18 #DreamJob to professional rafter",
"in_reply_to_status_id": null,
"id": 421584490452893696,
"favorite_count": 0,
"source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Mobile Web (M2)</a>",
"retweeted": false,
"coordinates": null,
"entities": {
"symbols": [],
"user_mentions": [
{
"id": 183093247,
"indices": [
0,
11
],
"id_str": "183093247",
"screen_name": "HomeShop18",
"name": "HomeShop18"
}
],
"hashtags": [
{
"indices": [
12,
21
],
"text": "DreamJob"
}
],
"urls": []
},
"in_reply_to_screen_name": "HomeShop18",
"id_str": "421584490452893696",
"retweet_count": 0,
"in_reply_to_user_id": 183093247,
"favorited": false,
"user": {
"follow_request_sent": null,
"profile_use_background_image": true,
"default_profile_image": false,
"id": 2254546045,
"verified": false,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg",
"profile_sidebar_fill_color": "171106",
"profile_text_color": "8A7302",
"followers_count": 87,
"profile_sidebar_border_color": "BCB302",
"id_str": "2254546045",
"profile_background_color": "0F0A02",
"listed_count": 1,
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"utc_offset": null,
"statuses_count": 9793,
"description": "Rafter. Rafting is what I do. Me aur mera Tablet. Technocrat of Future",
"friends_count": 231,
"location": "",
"profile_link_color": "473623",
"profile_image_url": "http://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg",
"following": null,
"geo_enabled": false,
"profile_banner_url": "https://pbs.twimg.com/profile_banners/2254546045/1388065343",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"name": "Jayy",
"lang": "en",
"profile_background_tile": false,
"favourites_count": 41,
"screen_name": "JzayyPsingh",
"notifications": null,
"url": null,
"created_at": "Fri Dec 20 05:46:00 +0000 2013",
"contributors_enabled": false,
"time_zone": null,
"protected": false,
"default_profile": false,
"is_translator": false
},
"geo": null,
"in_reply_to_user_id_str": "183093247",
"lang": "en",
"created_at": "Fri Jan 10 10:09:09 +0000 2014",
"filter_level": "medium",
"in_reply_to_status_id_str": null,
"place": null
}
在下面的示例中可以看到,json.loads
(和json.load
)不对多个json对象进行解码。
>>> json.loads('{}')
{}
>>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({}))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 368, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 3 - line 1 column 5 (char 2 - 4)
如果要转储多个字典,请将它们包装在列表中,转储列表(而不是多次转储字典)
>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]
使用表情面板增加 mBot 的可玩性。 表情面板可通过 RJ25 线连接到主板上对应的接口。 坐标 如上图所示,表情面板以左上角为坐标 0 点, x ,y 的方向如箭头示意。参数有效范围: x: -15 ~ 15 y: -7 ~ 7 1. 表情面板(接口1)显示图案()持续()秒 在指定接口连接的表情面板显示指定图案,持续一段时间后熄灭。 示例: 按下空格键,mBot 的外接表情面板会显示指定图案
Composite Images watermark(['/img/shepherd.jpg', '/img/logo.png']) .image(watermark.image.lowerRight()) .then(function (img) { document.getElementById('composite-image').appendChild(img); }); Al
在 MShowInstDetails show ShowUnInstDetails show 下面添加以下代码: ShowInstDetails hide ;安装过程,该项默认为show,hide表示不显示安装信息,点击显示按钮后显示。 ShowUnInstDetails hide ;卸载过程,该项默认为show,hide表示不显示安装信息,点击显示按钮后显示。 效果如图:
void display(string template [, string cache_id [, string compile_id]]) This displays the template. Supply a valid template resource type and path. As an optional second parameter, you can pass a cach
显示文字或者表情。 用法 Your browser does not support the video tag. 案例:保险箱 说明:使用旋钮制作密码器,通过显示屏显示密码。输入密码正确后指示灯亮起,按下按钮保险箱被开启。 所需模块:电源、蓝牙模块、按钮、旋钮、显示屏、双舵机驱动、舵机驱动包、转接线10cm。
显示屏模块能够显示输入值。你也可以通过编程自定义要显示的信息,如数字、标点和表情信息等。 净重量:12.4g 体积:24×48×14mm 参数 工作电压:DC 5V 抗跌落能力:1.5m 工作温度:-10℃~55℃ 工作湿度:<95% 特点 支持图像、表情、ASCII编码字符等多种输出。
LED点阵屏幕 小程的屏幕是由128颗LED组成的点阵,可以显示英文字符、数字与图案。 坐标示意图 如上图所示,表情面板以左上角为坐标 0 点, x ,y 的方向如箭头示意。参数有效范围: x:-15 ~ 15 y:-7 ~ 7 1. 显示图案()()秒 在小程的屏幕上显示图案,持续一段时间后熄灭。图案和时间由用户自定义,鼠标单击可修改,默认值如下: 示例: 当按下按钮A时,小程的屏幕会依次显示这
8×16 蓝色LED点阵