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}

6
  • are you sure it's the right solution? is your json file corrupt? if so use str.partition("[")[0] Commented Mar 10, 2017 at 13:37
  • @Jean-François Fabre OK but how can i implement this to code? I mean I have a variable witch store string and I want to truncate this variable. Commented Mar 10, 2017 at 14:01
  • Why did you tag your question with batch-file? If you need a batch-file solution: supposing the string is stored in variable %STRING%, use %STRING:*[=[% returns the first [ and everything behind... Commented Mar 10, 2017 at 14:03
  • @aschipfl but what to do with a file? Commented Mar 10, 2017 at 14:38
  • What do you mean? the string is stored in a file, or the result is to be written to a file, or both? Commented Mar 10, 2017 at 14:40

3 Answers 3

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"]
Sign up to request clarification or add additional context in comments.

12 Comments

but it isn't working when string is stored in variable
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 ?
yeah you are right, so what I can do? error = 'dict' object has no attribute 'find'
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 ?
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.
|
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!

4 Comments

I got invalid syntax error unfortunately.
There is no syntax error in the answer. Can you share the line that you get the error!?
<--the colon what is that ??
I guess that we have to extract text from dict
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

6 Comments

this will throw list index out of range error if the string doesn't have '[' in it!
@Darkaird but it isn't working when string is stored in variable
@lukesky what do you mean? It should work whether or not it is stored in a variable.
@Darkaird AttributeError: 'dict' object has no attribute 'split'
and NameError: name 'index' is not defined
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.