行が次のような大きな CSV ファイルがあります。
id_85,
{
"link": "some link",
"icon": "hello.gif",
"name": "Wall Photos",
"comments": {
"count": 0
},
"updated_time": "2012-03-12",
"object_id": "400",
"is_published": true,
"properties": [
{
"text": "University",
"name": "By",
"href": "some link"
}
],
"from": {
"id": "7778",
"name": "Let"
},
"message": "Hello World! :D",
"id": "id_85",
"created_time": "2012-03-12",
"to": {
"data": [
{
"id": "100",
"name": "March"
}
]
},
"message_tags": {
"0": [
{
"id": "100",
"type": "user",
"name": "Marcelo",
"length": 7,
"offset": 0
}
]
},
"type": "photo",
"caption": "Hello world!"
}
最初と最後の中括弧の間のjson部分を取得しようとしています。
以下はこれまでの私のpython正規表現コードです
import re
str = "id_85,{"link": "some link", "icon": "hello.gif", "name": "Wall Photos", "comments": {"count": 0}, "updated_time": "2012-03-12", "object_id": "400", "is_published": true, "properties": [{"text": "University", "name": "By", "href": "some link"}], "from": {"id": "777", "name": "Let"}, "message": "Hello World! :D", "id": "id_85", "created_time": "2012-03-12", "to": {"data": [{"id": "100", "name": "March"}]}, "message_tags": {"0": [{"id": "100", "type": "user", "name": "March", "length": 7, "offset": 0}]}, "type": "photo", "caption": "Hello world!"} "
m = re.match(r'.*,({.*}$)', str)
if m:
print m.group(1)
{ ... } のように、最初と最後の中括弧を取らない場合があります。最初と最後の中括弧の間のテキストのみが含まれ、他のテキストは含まれないようにするにはどうすればよいですか?
目的の出力は次のようになります。
{"link": "いくつかのリンク", "icon": "hello.gif", "name": "Wall Photos", "comments": {"count": 0}, "updated_time": "2012-03- 12", "object_id": "400", "is_published": true, "properties": [{"text": "University", "name": "By", "href": "some link"}], "from": {"id": "777", "name": "Let"}, "message": "Hello World! :D", "id": "id_85", "created_time": "2012-03 -12", "to": {"data": [{"id": "100", "name": "3月"}]}, "message_tags": {"0": [{"id": " 100","type": "user", "name": "March", "length": 7, "offset": 0}]}, "type": "photo", "caption": "Hello world!"}
ありがとう!