How can I convert a hex value "0000.0012.13a4" into "00:00:00:12:13:A4"?
text = '0000.0012.13a4'
text = text.replace('.', '').upper() # a little pre-processing
# chunk into groups of 2 and re-join
out = ':'.join([text[i : i + 2] for i in range(0, len(text), 2)])
print(out)
00:00:00:12:13:A4
import re
old_string = "0000.0012.13a4"
new_string = ':'.join(s for s in re.split(r"(\w{2})", old_string.upper()) if s.isalnum())
print(new_string)
OUTPUT
> python3 test.py
00:00:00:12:13:A4
>
Without modification, this approach can handle some other MAC formats that you might run into like, "00-00-00-12-13-a4"
Try following code
import re
hx = '0000.0012.13a4'.replace('.','')
print(':'.join(re.findall('..', hx)))
Output: 00:00:00:12:13:a4
There is a pretty simple three step solution:
First we strip those pesky periods.
step1 = hexStrBad.replace('.','')
Then, if the formatting is consistent:
step2 = step1[0:2] + ':' + step1[2:4] + ':' + step1[4:6] + ':' + step1[6:8] + ':' + step1[8:10] + ':' + step1[10:12]
step3 = step2.upper()
It's not the prettiest, but it will do what you need!
It's unclear what you're asking exactly, but if all you want is to make a string all uppercase, use .upper()
Try to clarify your question somewhat, because if you're asking about converting some weirdly formatted string into what looks like a MAC address, we need to know that to answer your question.
Related
have files with date attached in last part of filename. Like this.
string = 'blablablabla_20210812.jpg'
I extract that data like this.
string[-12:][:-4]
I want to add '-' between year, month and date. This is how I do.
string[-12:][:-4][:4] + '-' + string[-12:][:-4][4:][:2] + '-' + string[-12:][:-4][6:]
In my opinion, it seems like more complicated than reading machine code. Could you guys could enlighten me the ways which are more pragmatic?
One solution is to use regular expression and re.sub:
import re
s = "blablablabla_20210812.jpg"
s = re.sub(r"_(\d{4})(\d{2})(\d{2})\.", r"_\1-\2-\3.", s)
print(s)
Prints:
blablablabla_2021-08-12.jpg
You can also replace the values joining the groups with lambda:
>>> import re
>>> string = 'blablablabla_20210812.jpg'
>>> re.sub('(\d{4})(\d{2})(\d{2})', lambda m: '-'.join(g for g in m.groups()), string)
#output: 'blablablabla_2021-08-12.jpg'
You can put both indices in one square and use join function:
'-'.join([string[-12:-8], string[-8:-6], string[-6:-4]])
Also, I personally prefer to keep code readable. You can name the variables first:
def extractDataInfo(string):
year, month, day = string[-12:-8], string[-8:-6], string[-6:-4]
return '-'.join([year, month, day])
You can compress the string[-12:][:-4][:4] to string[-12:-8] to make it look cleaner. The code will look like this:
string = 'blablablabla_20210812.jpg'
print(string[-12:-8] + '-' + string[-8:-6]+ '-' + string[-6:-4])
# 2021-08-12
Or this if you want the text:
print(string[:-8] + '-' + string[-8:-6]+ '-' + string[-6:-4])
# blablablabla_2021-08-12
A simple solution for this question will work only if you know that the file always ends with:
_data.extension
If so, the solution will be:
string = 'blablablabla_20210812.jpg'
s = string.replace("_", ".")
s = s.split(".")
data = s[1]
s[1] = data[:4] + "-" + data[4:6] + "-" + data[6:]
print(s[1]) # OUTPUT: 2021-08-12
# Taking all together:
print(s[0] + "_" + s[1] + "." + s[2]) # OUTPUT: blablablabla_2021-08-12.jpg
You can also try like this:
import datetime
print(datetime.datetime.strptime(string[-12:-4],'%Y%m%d').strftime('%Y-%m-%d'))
Output:
'2021-08-12'
I have files with names like centerOne_camera_2_2018-04-11_15:11:21_2.0.jpg. I want to change the last string i.e. image_name.split('_')[5].split('.')[0] to some other string. I can't seem to find a neat way to do this and ended up doing the following which is very crude
new_name = image_base.split('_')[0] + image_base.split('_')[1] + image_base.split('_')[2] + image_base.split('_')[3] + image_base.split('_')[4] + frameNumber
That is, my output should be centerOne_camera_2_2018-04-11_15:11:21_<some string>.0.jpg
Any better way is appreciated. Note: I want to retain the rest of the string too.
I think you may be looking for this:
>>> "centerOne_camera_2_2018-04-11_15:11:21_2.0.jpg".rpartition("_")
('centerOne_camera_2_2018-04-11_15:11:21', '_', '2.0.jpg')
That is for the last element. But from the comments I gather you want to split at delimiter n.
>>> n = 3
>>> temp = "centerOne_camera_2_2018-04-11_15:11:21_2.0.jpg".split("_",n)
>>> "_".join(temp[:n]),temp[n]
('centerOne_camera_2', '2018-04-11_15:11:21_2.0.jpg')
I'm not sure what your objection to using + is, but you can do this if you like:
>>> temp="centerOne_camera_2_2018-04-11_15:11:21_2.0.jpg".rpartition("_")
>>> "{0}<some_string>{2}".format(*temp)
'centerOne_camera_2_2018-04-11_15:11:21<some_string>2.0.jpg'
You can try rsplit:
"centerOne_camera_2_2018-04-11_15:11:21_2.0.jpg".rsplit("_", 1)
['centerOne_camera_2_2018-04-11_15:11:21', '2.0.jpg']
I am having a problem with a + sign.
Here is my format of my CSV
wifichannelnumber+ssid+macaddress of AP
Here is an example of a good line
6+Jills-Equinox+78:61:7c:19:xx:xx
And here is the problem mine. Note the + next to S8.
11+Samsung-Galaxy-S8+-4469+a2:cc:2b:8d:xx:xx
I would like to remove plus in bash or python (Edit) for a whole CSV
phone = "11+Samsung-Galaxy-S8+-4469+a2:cc:2b:8d:xx:xx"
print(phone.replace("S8+","S8"))
>>>11+Samsung-Galaxy-S8-4469+a2:cc:2b:8d:xx:xx
Your desire regex is:
^(\d+)\+(.*)\+(([\w\d]{2}\:){5}[\d\w]{2})$
Then you can use python to remove every '+' sign in the second regex group
Python solution:
s = '11+Samsung-Galaxy-S8+-4469+a2:cc:2b:8d:xx:xx'
if s.count('+') > 2:
parts = s.split('+')
s = '{}+{}+{}'.format(parts[0], ''.join(parts[1:-1]), parts[-1])
print(s)
The output:
11+Samsung-Galaxy-S8-4469+a2:cc:2b:8d:xx:xx
The following Python script: re.sub("[^a-zA-Z]pi[^a-zA-Z]", "(math.pi)", "2pi3 + supirse")
results in: '(math.pi) + supirse'
While the match of a non-alpha before and after pi is critical, I do not want these non-alpha characters to be replaced in the match. I would like to see the following output: '2(math.pi)3 + supirse'
Note: A previous suggestion of the following: re.sub("\Bpi\B", "(math.pi)", "2pi3 + supirse")
results in a complete replacement of every instance: '2(math.pi)3 + su(math.pi)rse' which is also NOT what I am looking for
Use this instead: re.sub("(?<=[^a-zA-Z])pi(?=[^a-zA-Z])", "(math.pi)", "2pi3 + supirse")
Visualization: http://regex101.com/r/fX5wX3
Use lookahead/lookbehind:
import re
print re.sub("(?<=[^a-zA-Z])pi(?=[^a-zA-Z])", "(math.pi)", "2pi3 + supirse")
See here for the concrete produced result: http://ideone.com/rSd8H
In fact you need a lowercase "\b" that means word boundary, while "\B" means not a word boundary.
Try this:
import re
re.sub(r"\bpi\b", "(math.pi)", "2pi3 + supirse")
That would yield '2pi3 + supirse'
I have an access table that has a bunch coordinate values in degrees minutes seconds and they are formatted like this:
90-12-28.15
I want to reformat it like this:
90° 12' 28.15"
essentially replacing the dashes with the degrees minutes and seconds characters and a space between the degrees and minutes and another one between the minutes and seconds.
I'm thinking about using the 'Replace' function, but I'm not sure how to replace the first instance of the dash with a degree character (°) and space and then detect the second instance of the dash and place the minute characters and a space and then finally adding the seconds character at the end.
Any help is appreciated.
Mike
While regular expressions and split() are fine solutions, doing this with replace() is rather easy.
lat = "90-12-28.15"
lat = lat.replace("-", "° ", 1)
lat = lat.replace("-", "' ", 1)
lat = lat + '"'
Or you can do it all on one line:
lat = lat.replace("-", "° ", 1).replace("-", "' ", 1) + '"'
I would just split your first string:
# -*- coding: utf-8 -*-
str = '90-12-28.15'
arr = str.split('-')
str2 = arr[0] +'° ' + arr[1] + '\'' +arr[2] +'"'
print str2
You might want to use Python's regular expressions module re, particularly re.sub(). Check the Python docs here for more information.
If you're not familiar with regular expressions, check out this tutorial here, also from the Python documentation.
import re
text = 'replace "-" in 90-12-28.15'
print(re.sub(r'(\d\d)-(\d\d)-(\d\d)\.(\d\d)', r'''\1° \2' \3.\4"''', text))
# use \d{1,2} instead of \d\d if single digits are allowed
The python "replace" string method should be easy to use. You can find the documentation here.
In your case, you can do something like this:
my_str = "90-12-28.15"
my_str = my_str.replace("-","°",1)# 1 means you are going to replace the first ocurrence only
my_str = my_str.replace("-","'",1)
my_str = my_str + "\""