How to print only int in python from a sentence? - python-3.x

'>>>[123.456, 789.123]
Here i only need output as "123.456, 789.123"
in python what should i do for that?
Previously i had tried this
import geocoder
import re
g = geocoder.ip('me')
x = print(g.latlng)
string = (x)
string = re.sub('[^0-9]', '', string)
print (string)
In fact i want to print my current location so i had first code something like this...
import geocoder
g = geocoder.ip('me')
print(g.latlng)
but this only show the the latitude and longitude
in square brackets [].
So i need those output without brackets because i want to directly find all details using this outputs and that program is not accepting the inputs in square brackets.
I hope you understand so please help me.

You are getting an array not a string!
lat and lng is what you will need to pass
import geocoder
g = geocoder.ip('me')
lat = g.latlng[0]
lng = g.latlng[1]
print(str(lat) + ", " + str(lng))

Related

Python split currency string into currency code and amount

I am trying to split R15.49 to (R, 15.49) or
ZAR15.49 to (ZAR, 15.49)
I have tried one of the solutions here and implememted the function below:
def splitCurrency(string):
match = re.search(r'([\D]+)([\d,]+)', string)
output = (match.group(1), match.group(2).replace(',',''))
return output
But I am getting (R, 15) or (ZAR, 15). and its ignoring the digits after the decimal place
If you want to fish out these values from a larger text, then use re.findall:
inp = "R15.49 or ZAR15.49"
matches = re.findall(r'\b([A-Z]+)(\d+(?:\.\d+)?)\b', inp)
print(matches)
This prints:
[('R', '15.49'), ('ZAR', '15.49')]

Need help converting string or int to float

So I have been stuck on this one for a while and could use some help. I have been trying to fix this code and I keep getting an error about invalid syntax. So here is the code I need help with need to convert from str or int to float.
# Input from the command line
import sys
A = sys.argv[1]
B = sys.argv[2]
C = sys.argv[3]
# Your code goes here
num = A * (B + C / 3)
# Outputs
print (float(num))
By default,python gets inputs from command line as str.So,you must convert them to floats before applying any operation
import sys
A=float(sys.argv[1])
B=float(sys.argv[2])
C=float(sys.argv[3])
.....

Python - check if string contains special characters, if yes, replace them

I am having a number of strings in the loop, some strings contain "," and some doesn't contain, I want my code to check if there is any "," present in a string remove them and then print the string, and if its not present, print the string as it is.
here is my code:
for x in range(y):
c = containers[x].find("div",{"class":"cong-data"})
meeting = c.p.next_element
print(meeting)
Thanks in advance.
I recommend using Regular Expression sub() function.
import re
string = 'aabcdefg,hijklmnop,qrstu,ssads'
remove_commas = re.sub(',', '', string)
print(string)
print(remove_commas)
output:
aabcdefg,hijklmnop,qrstu,ssads
aabcdefghijklmnopqrstussads

Python: Add an alphabet to a string

Am tryin to add all the alphabet to all the position in a string just one by one, This is the code:
from string import ascii_lowercase
var = 'abc'
for i in ascii_lowercase:
result = [var[:j] + i + var[j:] for j in range(len(var))]
But this is what am getting :
['zabc', 'azbc', 'abzc']
This is what am expecting :
['aabc', 'abac', 'abca','babc','abbc','abcb'...]
Does anyone know how to fix this. Thanks.
You can build the whole list at once using a nested list comprehension
from string import ascii_lowercase
var = 'abc'
result = [var[:n]+c+var[n:] for c in ascii_lowercase for n in range(len(var)+1)]

How to split data and save min value of startTime to variable

i wan't to create the script which can save from csv startTime and endTime to variable, for example i have different startTime values(11:59:12, 11:59:19, 11:59:22) and different endTime values (12:59:12, 12:59:19, 12:59:22), i want to store for startTime to varible only lowest one, which has been started early than others and endTime which has been ended last.
So from this examples must be stored to variable startTime = 11:59:12 and endTime = 11:59:22.
Could you tell me please what i gonna do. my script looks right now like that:
import csv
import datetime
import time
data = list(csv.reader(open('C:\\Documents and Settings\\Lemur\\Desktop\\TestResults.csv', 'r')))
startTimeWithoutCp = ""
for x in xrange(4, 13):
cpStartTime = (data[x][6])+ ":" + " " + (data[x][12][10:19])
cpEndTime = (data[x][6])+ ":" + " " + (data[x][13][10:19])
startTimeWithoutCp += (data[x][12][11:19])
#print("CP Start Times is: ", cpStartTime)
#print("CP End Time is: ", cpEndTime)
print(startTimeWithoutCp)
# The output results looks like that:
# 11:59:0811:59:1211:59:1911:59:2211:59:2811:59:3211:59:3711:59:4211:59:47
Could you tell me please, how i can split correctly this results and get minimal value from this list. Thank you!
First of all, I think you should store your time points in a list. Concatenating them into one string (like you do it for startTimeWithoutCp) makes it hard to get back the original times. On individual time points, you can use the fact that strings / characters can be sorted:
>>> a = '11:20:02'
>>> b = '04:34:12'
>>> c = '04:34:14'
>>> sorted([a, b, c])
['04:34:12', '04:34:14', '11:20:02']
>>> min([a, b, c])
'04:34:12'
So given you have your time points in a list, you can just use min() and max().
You can split the long string into its pieces by using Python's slicing notation:
d = '11:59:0811:59:1211:59:1911:59:2211:59:2811:59:3211:59:3711:59:4211:59:47'
print([d[i:i + 8] for i in range(0, len(d), 8)])

Resources