Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am importing currency exchange rates from a website. All is well, except python prints the required data as follows:
['12.3098']
which means I can't use the data that I scraped in any calculations in my program. How do I get rid of the square brackets so that I can convert the string inside the square brackets to a float?
you have a list which is not a string... so if you want to get that value out select it like so
float(result[0])
replace 'result' with whatever your object is, aka what you printed ['12.3098']
try printing the type of your object type(result) and if its a list then this will fix your problem
if the type is a string you can do a literal evaluation of it like this
import ast
result = ast.literal_eval(result)
print result[0]
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Error message:
alarm = f"{alarm_hour}:{alarm_minute:02}:{alarm_am_pm}"
ValueError: '=' alignment not allowed in string format specifier
I don't know why this code is giving the error as I have used the same before in the 's program and it's working fine. Here's the code.
TIME = f"{current_hour}:{current_minute:02}:{current_second} {am_pm}"
But whenever I remove :02 from alarm_minute it works as I wanted to show number in 2 digits. I'm confused what's wrong with the code. Both are almost identical but one is giving error other's don't.
Assuming you want to have the number formated as a two digit with leading 0 when its a single digit you can pass this with the letter d to let the fstring know you want to format as 2 digits leaded by 0. you can do similar with floats to print to x many decimal places.
num = 3
price = 4.9
print(f"{num:02d}, {price:.2f}")
OUTPUT
03, 4.90
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I try to convert a list of Chinese province into pinyin use pinyin package, code like below:
df['province'] = df['comb_province'].apply(lambda x: pinyin.get(x, format="strip", delimiter=''))
but I got an error says: 'float' object is not iterable. Why this happens? How can I fix it?
Thank you!
You may have been encountered numpy.nan or None values in the df["comb_province"] column. So, you could try to remove those rows with numpy.nan by using the following code:
df = df[~df["comb_province"].isnull()]
or if you wish to keep the rows with numpy.nan or None, then using the following:
df["comb_province"] = df["comb_province"].astype(str)
Your original code may be of strip or split, something related to string operations, which will throw an error when encountering either numpy.nan or None.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How do I search and replace using built-in Python methods?
For instance, with a string of appleorangegrapes (yes all of them joined),
Replace "apple" with "mango".
The .replace method only works if the words are evenly spaced out but not if they are combined as one. Is there a way around this?
I searched the web but again the .replace method only gives me an example if they are spaced out.
Thank you for looking at the problem!
This works exactly as expected and advertised. Have a look:
s = 'appleorangegrapes'
print(s) # -> appleorangegrapes
s = s.replace('apple', 'mango')
print(s) # -> mangoorangegrapes
The only thing that you have to be careful of is that replace is not an in-place operator and as such it does not update s automatically; it only creates a new string that you have to assign to something.
s = 'appleorangegrapes'
s.replace('apple', 'mango') # the change is made but not saved
print(s) # -> appleorangegrapes
replace can work for any string, why you think that it doesn't, here is the test:
>>> s='appleorangegrapes'
>>> s.replace('apple','mango')
'mangoorangegrapes'
>>>
Don't you see that you received your expected result?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I have a list of chars like
nodeList = ['A','C','E','G']
and I want to extract the A. So I found the list[number] method for extracting from a list. But when I put
node = nodeList[0]
I get an error saying that "'dict_keys' object does not support indexing." So how can I work around this? Thanks.
As stated in the comments, nodeList is not actually a list, but a dict_keys object. Before trying to index it, you may simply convert it to a list:
nodeList = list(nodeList)
node = nodeList[0]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a .txt file that has a really long RNAm sequence. I don´t know the exact length of the sequence.
What I need to do is extract the part of the sequence that is valid, meaning it starts with "AUG" and ends in "UAA" "UAG" or "UGA". Since the sequence is too long I don´t know the index of any of the letters or where the valid sequence is.
I need to save the new sequence in another variable.
Essentially, what you need to do, without coding the whole thing for you, is:
Example string:
rnaSequence = 'ACGUAFBHUAUAUAGAAAAUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAAAAAGGGUAUAUAGAUGAGAGAGA'
You will want to find the index of the 'AUG' and the index of 'UAA', 'UAG', or 'UGA' .. Something like this
rnaStart = rnaSequence.index(begin)
Then you'll need to set the slice of the string to a new variable
rnaSubstring = rnaSequence[rnaStart:rnaEnd+3]
Which in my string above, returns:
AUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAA