Remove brackets from list [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 years ago.
I have some problems with using a list in python.
Right now, I open a .txt file with data, and read it into my python file.
However, when I put the input from the datafile into variable data and print this to check if it works or not, I see a lot of extra brackets which I don't want. Right now, it looks like:
["['sports','pizza','other']"]
and I want it to have it in a way like this:
['sports','pizza','other']
Can someone help me to get this work? Reason why I want it in a format like I mentioned above, is that I want to compare the list with another list, and that does not work in the format with the ]"]
I hope someone will help me.

Simply use eval function from Python.
>>> a = ["['sports','pizza','other']"]
>>> eval(a[0])
['sports', 'pizza', 'other']

Related

strange python dictionary? [duplicate]

This question already has answers here:
What does the 'b' character do in front of a string literal?
(11 answers)
Convert a bytes array into JSON format
(8 answers)
Closed 2 years ago.
new to python here. Been trying this for quite a while now; would really appreciate some help.
I thought to try my hand at pulling data from an api, and what i got is a very long dictionary ( or at least i think its an dictionary )
response = requests.get('https://api.coingecko.com/api/v3/finance_products')
print(response.content)
can anyone give me any pointers on how to manipulate the results ?
I can't call the dictionary.
First few characters of it :
b'[{"platform":"Binance Savings","identifier":"CCOCOS30DAYSS001","supply_rate_percentage":"6.0","borrow_rate_percentage":null,"number_duration":null,"length_duration":null,"start_at":0,"end_at":0,"value_at":0,"redeem_at":0},{"platform":"DDEX Lending","identifier": etc etc
i'm not sure why there is a b' at the front.
Sorry if this isn't a clear question.
You can use
response.json()
which decodes it immediatly to a json object. Documentation here

Generate multiple OR/AND statements [duplicate]

This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 4 years ago.
I am working on python script that splits text in different blocks based on keywords used in text.
Currently I split text into blocks with sth like this (for 1 block, others have pretty much the same strucure):
if (line.strip().lower().startswith('ключевые навыки')
or line.strip().lower().startswith('дополнительная информация')
or line.strip().lower().startswith('знания')
or line.strip().lower().startswith('личные качества')
or line.strip().lower().startswith('профессиональные навыки')
or line.strip().lower().startswith('навыки')):
But, it is possible that list of keywords is going to expand. Is there a possibility to generate multiple or statements based on some array of possible keywords?
Try this code
values=['ключевые навыки','дополнительная информация','знания']
val=True
#enter any words you want to check
while val
for i in values:
if (line.strip().lower().startswith(i)):
#whatever code you want to implement
val=False
#to exit loop
Hope it helps :)

How to find the position of every instance of a character in a list [duplicate]

This question already has answers here:
How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)
(3 answers)
Closed 6 years ago.
I have a program where I need to identify the location of every instance of the letter A in a quote. Something like I would do with quote.index("A"), but I need every instance of A, not just the first.
I know this question has been asked before but I'm very, very new to Python and I'm having trouble understanding the answers to those questions.
If anyone could give me a dumbed down explanation of how to do this, I'd be incredibly thankful because I'm utterly lost.
If i understand correctly, you have a string and you want to keep all A's locations in e different array.
Then you can try something like that.
quote = "some quote"
locs = []
for i in range(len(quote)) :
if quote[i] == 'A' :
locs.append(i)
print(locs)

Splitting an input in to smaller fragments [duplicate]

This question already has an answer here:
Splitting an input in to fragments (Python)
(1 answer)
Closed 9 years ago.
I need to somehow convert a mathematical input(str) to a number,
e.g.
4-3*2-1+5 = ((((4-3)*2)-1)+5).
Current code looks like this:
Answer = input ('Put your answer here: ')
4-3*2-1+5
Somehow, I need to remake the string in to smaller fragments so that it reads from left to right, and to remake the numbers in to integers, but I have no idea how to do it.
I tried doing
Answer.split('+','-','*','/')
But it says TypeError: split() takes at most 2 arguments (4 given)
Also tried adding the answer to a list to see if that helped me at all:
li.append(Answer)
(li = ['4-3*2-1+5']
But I don't see anything beneficial with that..
Please help!
(I'm new to SOF, so if there's any information that's missing, please tell me what and I will try to correct it).
What you need to write is a parser and simple evaulator for simple expressions.
I would start reading any of the following:
http://pyparsing.wikispaces.com/HowToUsePyparsing
http://pyparsing.wikispaces.com/Examples
http://kmkeen.com/funcparserlib/
There are many other parser libraires, but these are just a couple.
You could also just use the rply library which if you have a look at the PyPi page has an example that directly implement and simple expression parser and evaluater just like what you're describing in your question.

How can I convert words in [ ] to words in line? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to join list of strings?
I have a couple of functions in a module to run. First, I have to use read() to read strings in a document and save it as new_string and run a function. Then, I need to read a document using readlines(). After running the first function like match = clean_passage(new_string) then match contains ['/n', 'asdf', 'dfg']. Now, I need them in a line as it is shown in the original document. So, asdf dfg. How can I convet match into a thing that contains strings in a similar fashion that we get when we read a document using readlines().
So far, to do this, I had to save it and then open it using readlines(), which takes time. Is there any way to do that using a simple command? Sorry if the explanation is not clear.
try this:
to_convert = ['/n', 'asdf', 'dfg']
original_line = " ".join(to_convert)

Categories