Python Mac Address Convert Format from : to - [duplicate]
This question already has an answer here:
Replacing instances of a character in a string
9 answers
i have a .txt file containing mac address with this format f2:e0:e2:e8:3a:5e
how can i convert f2:e0:e2:e8:3a:5e to f2-e0-e2-e8-3a-5e using pyhton and use it as variable?
python macos
marked as duplicate by timgeb, RoadRunner, usr2564301, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 12:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Replacing instances of a character in a string
9 answers
i have a .txt file containing mac address with this format f2:e0:e2:e8:3a:5e
how can i convert f2:e0:e2:e8:3a:5e to f2-e0-e2-e8-3a-5e using pyhton and use it as variable?
python macos
marked as duplicate by timgeb, RoadRunner, usr2564301, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 12:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
just replace every:
with a-
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51
add a comment |
This question already has an answer here:
Replacing instances of a character in a string
9 answers
i have a .txt file containing mac address with this format f2:e0:e2:e8:3a:5e
how can i convert f2:e0:e2:e8:3a:5e to f2-e0-e2-e8-3a-5e using pyhton and use it as variable?
python macos
This question already has an answer here:
Replacing instances of a character in a string
9 answers
i have a .txt file containing mac address with this format f2:e0:e2:e8:3a:5e
how can i convert f2:e0:e2:e8:3a:5e to f2-e0-e2-e8-3a-5e using pyhton and use it as variable?
This question already has an answer here:
Replacing instances of a character in a string
9 answers
python macos
python macos
asked Nov 24 '18 at 10:42
nicollette16nicollette16
125
125
marked as duplicate by timgeb, RoadRunner, usr2564301, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 12:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by timgeb, RoadRunner, usr2564301, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 12:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
just replace every:
with a-
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51
add a comment |
1
just replace every:
with a-
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51
1
1
just replace every
:
with a -
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
just replace every
:
with a -
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51
add a comment |
2 Answers
2
active
oldest
votes
Open it with open()
, read contents to a string with the .read()
method and replace colons with hyphens with the .replace()
string method. Store the result in a variable.
mac_addr = open('your_file.txt').read().replace(':', '-')
1
Nice. I would alsoclose()
the file afterwards, or use awith
context manager.
– RoadRunner
Nov 24 '18 at 10:48
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
add a comment |
Probably(from the idea/complexity) a little faster than Joe's answer(depends on implementation) :
If you can ensure that your address is always in the format xx:xx:xx:xx:xx:xx [...]
with open('your_file.txt') as file:
address=list(file.read())
for i in range(2, len(address), 2):
address[i]="-"
address="".join(address)
# do stuff with address here
using with
as proposed by RoadRunner.
And if you want it blazing fast, look at this :
Fast character replacing in Python's immutable strings
This solution will replace every 2nd character with a hyphen.
1
len(address)
? you can't get the length of raw file objects. Same goes for indexingaddress[i]="-"
.
– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster thanadress = address.replace(':', '-')
?
– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Open it with open()
, read contents to a string with the .read()
method and replace colons with hyphens with the .replace()
string method. Store the result in a variable.
mac_addr = open('your_file.txt').read().replace(':', '-')
1
Nice. I would alsoclose()
the file afterwards, or use awith
context manager.
– RoadRunner
Nov 24 '18 at 10:48
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
add a comment |
Open it with open()
, read contents to a string with the .read()
method and replace colons with hyphens with the .replace()
string method. Store the result in a variable.
mac_addr = open('your_file.txt').read().replace(':', '-')
1
Nice. I would alsoclose()
the file afterwards, or use awith
context manager.
– RoadRunner
Nov 24 '18 at 10:48
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
add a comment |
Open it with open()
, read contents to a string with the .read()
method and replace colons with hyphens with the .replace()
string method. Store the result in a variable.
mac_addr = open('your_file.txt').read().replace(':', '-')
Open it with open()
, read contents to a string with the .read()
method and replace colons with hyphens with the .replace()
string method. Store the result in a variable.
mac_addr = open('your_file.txt').read().replace(':', '-')
answered Nov 24 '18 at 10:44
Joe IddonJoe Iddon
15.3k31639
15.3k31639
1
Nice. I would alsoclose()
the file afterwards, or use awith
context manager.
– RoadRunner
Nov 24 '18 at 10:48
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
add a comment |
1
Nice. I would alsoclose()
the file afterwards, or use awith
context manager.
– RoadRunner
Nov 24 '18 at 10:48
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
1
1
Nice. I would also
close()
the file afterwards, or use a with
context manager.– RoadRunner
Nov 24 '18 at 10:48
Nice. I would also
close()
the file afterwards, or use a with
context manager.– RoadRunner
Nov 24 '18 at 10:48
2
2
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
@RoadRunner Yes, you could but the file object is now detached from any variable, so we can't throw an error on it and since Python will close the file eventually, I'm happy to leave it. :)
– Joe Iddon
Nov 24 '18 at 10:56
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
tested this code and it work great. thank your time @JoeIddon
– nicollette16
Nov 24 '18 at 11:40
add a comment |
Probably(from the idea/complexity) a little faster than Joe's answer(depends on implementation) :
If you can ensure that your address is always in the format xx:xx:xx:xx:xx:xx [...]
with open('your_file.txt') as file:
address=list(file.read())
for i in range(2, len(address), 2):
address[i]="-"
address="".join(address)
# do stuff with address here
using with
as proposed by RoadRunner.
And if you want it blazing fast, look at this :
Fast character replacing in Python's immutable strings
This solution will replace every 2nd character with a hyphen.
1
len(address)
? you can't get the length of raw file objects. Same goes for indexingaddress[i]="-"
.
– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster thanadress = address.replace(':', '-')
?
– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
add a comment |
Probably(from the idea/complexity) a little faster than Joe's answer(depends on implementation) :
If you can ensure that your address is always in the format xx:xx:xx:xx:xx:xx [...]
with open('your_file.txt') as file:
address=list(file.read())
for i in range(2, len(address), 2):
address[i]="-"
address="".join(address)
# do stuff with address here
using with
as proposed by RoadRunner.
And if you want it blazing fast, look at this :
Fast character replacing in Python's immutable strings
This solution will replace every 2nd character with a hyphen.
1
len(address)
? you can't get the length of raw file objects. Same goes for indexingaddress[i]="-"
.
– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster thanadress = address.replace(':', '-')
?
– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
add a comment |
Probably(from the idea/complexity) a little faster than Joe's answer(depends on implementation) :
If you can ensure that your address is always in the format xx:xx:xx:xx:xx:xx [...]
with open('your_file.txt') as file:
address=list(file.read())
for i in range(2, len(address), 2):
address[i]="-"
address="".join(address)
# do stuff with address here
using with
as proposed by RoadRunner.
And if you want it blazing fast, look at this :
Fast character replacing in Python's immutable strings
This solution will replace every 2nd character with a hyphen.
Probably(from the idea/complexity) a little faster than Joe's answer(depends on implementation) :
If you can ensure that your address is always in the format xx:xx:xx:xx:xx:xx [...]
with open('your_file.txt') as file:
address=list(file.read())
for i in range(2, len(address), 2):
address[i]="-"
address="".join(address)
# do stuff with address here
using with
as proposed by RoadRunner.
And if you want it blazing fast, look at this :
Fast character replacing in Python's immutable strings
This solution will replace every 2nd character with a hyphen.
edited Nov 24 '18 at 11:01
answered Nov 24 '18 at 10:47
LMDLMD
541414
541414
1
len(address)
? you can't get the length of raw file objects. Same goes for indexingaddress[i]="-"
.
– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster thanadress = address.replace(':', '-')
?
– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
add a comment |
1
len(address)
? you can't get the length of raw file objects. Same goes for indexingaddress[i]="-"
.
– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster thanadress = address.replace(':', '-')
?
– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
1
1
len(address)
? you can't get the length of raw file objects. Same goes for indexing address[i]="-"
.– RoadRunner
Nov 24 '18 at 10:55
len(address)
? you can't get the length of raw file objects. Same goes for indexing address[i]="-"
.– RoadRunner
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
@RoadRunner Oh, sorry, forgot read. Thanks.
– LMD
Nov 24 '18 at 10:55
This still won't work.
address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster than adress = address.replace(':', '-')
?– RoadRunner
Nov 24 '18 at 10:58
This still won't work.
address[i]="-"
will throw TypeError: 'str' object does not support item assignment. Are you sure that this will end up being faster than adress = address.replace(':', '-')
?– RoadRunner
Nov 24 '18 at 10:58
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
@RoadRunner not anymore ;)
– LMD
Nov 24 '18 at 11:02
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
thank you @LMD i will try this one also..
– nicollette16
Nov 24 '18 at 11:41
add a comment |
1
just replace every
:
with a-
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:42
@RoadRunner Nope, this works - as my answer should show - better(faster) than only by replacing - of course replacing works just fine, but if you have to apply this to many addresses(let's say he stores thousands of them in files) then this makes a difference. Worth noting that I also don't need a function call ;)
– LMD
Nov 24 '18 at 10:51