Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently I'm working with an API of an online game I play to build a tool, and in doing so I've encountered a problem. The API returns JSON files to the user. While working on creating a class that parses these JSON files for me I've realized I'd like to be able to format a number inside of one of them so instead of being "585677088.5" it's "585,677,088". This would be easy enough if the string just contained this number however this string contains a bunch of other text as well.
Here's a block of the text
loan:0
unpaidfees:-3510000
total:585677088.5
I'm using python to do this.
The only existing code I have in place is:
import urllib2
data = urllib2.urlopen("URL")
Like this:
>>> my_str = """loan:0
... unpaidfees:-3510000
... total:585677088.5"""
>>> map(lambda x: (x.split(":")[0], int(float(x.split(":")[-1]))), my_str.split("\n"))
[('loan', 0), ('unpaidfees', -3510000), ('total', 585677088)]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is one of the string that I got:
str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
what I want:
['Name', 'coordinates','CITIZENS','colonisation']
I'm trying to get word in string such as Name, coordinate, citizens, colonisation with their original case.
I tried split method to remove underscores and make them individual word.
,but it did not work well.
How can I do this?
Yo can use a regular expression for that:
import re
text ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
re.findall('_(\w*)_', text)
Note str is a built python function, don't use for variable names
A regex should do the trick:
import re
s = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
result = re.findall('_(\w+)_', s)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
import json
data = json.load(open("files1\data.json"))
def definitioner(w):
return data(w)
word = input("Enter the word you are looking for: ")
print(definitioner(word))
I am doing a course on UDEMY and after trying it myself it didn't work so I even copied the code to see if it was my code, couldn't figure out what the issue was, any help would be appreciated. I am running Python 3.8
Thanks.
You are calling data(w) like it's a function, but data is a dictionary. Use data.get(w) instead:
def definitioner(w):
return data.get(w)
That also allows you to specify what you would like returned by default if the word is not present, by adding a second argument:
def definitioner(w):
return data.get(w, 'Word not found!')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to get all keys' values that equal "url" ignoring nesting from a JSON file and then output them to a text file. How would I go about doing this?
I'm running Python 3.7 and cannot seem to find a solution.
r = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json')
j = r.json()
The result expected from this would be a text file filled with links from this json file.
https://launchermeta.mojang.com/v1/packages/31fa028661857f2e3d3732d07a6d36ec21d6dbdc/a1.2.3_02.json
https://launchermeta.mojang.com/v1/packages/2dbccc4579a4481dc8d72a962d396de044648522/a1.2.3_01.json
https://launchermeta.mojang.com/v1/packages/48f077bf27e0a01a0bb2051e0ac17a96693cb730/a1.2.3.json
etc.
Using requests library
import requests
response = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json').json()
url_list = []
for result in response['versions']:
url_list.append(result['url'])
print(url_list)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking to return all instances of the following in Python, but not sure how. As in, how can I search a String and print every time the following format is found:
<a href="[what I'm trying to return is here]" class="faux-block-link__overlay-link"
You need an HTML parser, like BeautifulSoup. Sample:
>>> from bs4 import BeautifulSoup
>>>
>>> s = 'link'
>>> BeautifulSoup(s, "html.parser").a["href"]
u"[what I'm trying to return is here]"
where .a is equivalent to .find("a"). Note that BeautifulSoup provides a convenient dictionary-like access to element attributes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've an issue here. I have a string and I want to extract parts of it using regex. Here is the string
{{name}} I love me some work {{hero}}
I want to extract
[{{name}}, {{hero}}]
also in a case where the string exist as
{{name} I love me some work {{hero, come in {here, this is right}
I still want to get
[{{name}, {{name, {here, right}]
I hope this makes sense. I am working with Python.
If you want ['{{name}}', '{{hero}}', '{{hero, come in {here, this is right}'] use #Avinash's regex.
If you want ['{{name}}', '{{hero}}', '{{hero', {here', 'right}'] use the following:
re.findall(r'{+\w+}*|{*\w+}+', s)
RegEX DEMO
Have you tried the following?
import re
s = '{{name} I love me some work {{hero, come in {here, this is right}'
print re.findall(r'\{.*?\}', s)