0

How can I remove letters before certain character? I need to remove every letter from following string until “[“ character and redirect output to .csv file.

{"__metadata": {"uri": loremipsum etc [ {rest of the document}

Squashman
  • 13,649
  • 5
  • 27
  • 36
lukesky
  • 29
  • 1
  • 9

3 Answers3

1

Find the position of '[' and get the string after that position

print s[s.find("[")+1:].strip()

Sample output:

{rest of the document}

Hope it helps!

Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
1

You can split from the first occurence and take the rest like :

>>> string = "Egg[spam:miam"
>>> string.split("[", 1)[1]
>>> spam:miam

OR

>>> string = "Egg[spam:miam"
>>> string[string.index("[") + 1:]
>>> spam:miam
Darkaird
  • 2,700
  • 4
  • 15
  • 29
1

As per your provided info and required fields, i will suggest that if your JSON data is in a file then you can use:

import json
data = {}
with open("path to json file") as f:
    data = json.loads(f.read())

or if your data is stored in string then you can simply do

data = json.loads("json data string")

now you have data in a python dictionary object. now you can easily get any field from the object e.g getting field "cuid" from the first object in entries list:

print data["entries"][0]["cuid"]

or alternatively you can loop on entries list and get all the fields you need e.g

for entry in data["entries"]:
    print entry["cuid"]
    print entry["name"]
    print entry["id"]
    print entry["type"]
SHAHS
  • 462
  • 1
  • 5
  • 13
  • but it isn't working when string is stored in variable – lukesky Mar 10 '17 at 13:59
  • It is working, as you can see I store it in a variable and tested it. From the code in your question, i guess your working on a dict not string , is it like this ? – SHAHS Mar 10 '17 at 14:43
  • yeah you are right, so what I can do? error = 'dict' object has no attribute 'find' – lukesky Mar 10 '17 at 14:47
  • ok, then it depends what you want to extract from the dict. ? is it the value of "uri" your want to extract or something else ? – SHAHS Mar 10 '17 at 14:49
  • share your dict or its structure and let us know what you want to extract out of it, then I can suggest a solution for that. – SHAHS Mar 10 '17 at 14:52
  • `{"__metadata": {"uri": "http://ip:port/vvv/vvv/folders/"}, "first": {"__deferred": {"uri": "http://ip:port/vvv/vvv/folders/?page=1&pagesize=50"}}, "last": {"__deferred": {"uri": "http://ip:port/vvv/vvv/folders/?page=1&pagesize=50"}}, "entries": [{"__metadata": {"uri": "http://ip:port/vvv/vvv/folders/13483"}, ` – lukesky Mar 13 '17 at 08:32
  • `"cuid": "AfbTJW3iTE1MkiLULzA6P58", "name": "foldername1", "description": "", "id": "13483", "type": "Folder", "ownerid": "12", "updated": "Wed Mar 01 09:14:23 CET 2017"}, {"__metadata": {"uri": "http://ip:port/vvv/vvv/folders/523"}, "cuid": "AS1oZEJAynpNjZIaZK2rc7g", "name": "foldername2", "description": "", "id": "523", "type": "Folder", "ownerid": "10", "updated": "Wed Jan 18 00:11:06 CET 2017"},` – lukesky Mar 13 '17 at 08:32
  • I want to extract all information, but mainly: cuid, name, description, id, type, ownerid, updated. – lukesky Mar 13 '17 at 08:33
  • I have updated my answer, I hope it will solve your problem. – SHAHS Mar 13 '17 at 09:59
  • Unfortunaltely I got this error :json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) – lukesky Mar 13 '17 at 11:03
  • This error means that your JSON object is not formatted correctly. check your JSON structure. try to print you JSON first. if the our is like u'a' then you need to convert your object to json using json.loads() function. – SHAHS Mar 13 '17 at 11:05
  • why you accepted the answer at first, if you want unaccepted it later on ? its waste of others time. – SHAHS Mar 17 '17 at 08:51