Python Mac Address Convert Format from : to - [duplicate]












-1
















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?










share|improve this question













marked as duplicate by timgeb, RoadRunner, usr2564301, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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


















-1
















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?










share|improve this question













marked as duplicate by timgeb, RoadRunner, usr2564301, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















-1












-1








-1









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?










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 10:42









nicollette16nicollette16

125




125




marked as duplicate by timgeb, RoadRunner, usr2564301, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















  • 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














2 Answers
2






active

oldest

votes


















2














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(':', '-')





share|improve this answer



















  • 1





    Nice. I would also close() the file afterwards, or use a with 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



















2














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.






share|improve this answer





















  • 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











  • @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













  • @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


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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(':', '-')





share|improve this answer



















  • 1





    Nice. I would also close() the file afterwards, or use a with 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
















2














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(':', '-')





share|improve this answer



















  • 1





    Nice. I would also close() the file afterwards, or use a with 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














2












2








2







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(':', '-')





share|improve this answer













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(':', '-')






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 10:44









Joe IddonJoe Iddon

15.3k31639




15.3k31639








  • 1





    Nice. I would also close() the file afterwards, or use a with 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





    Nice. I would also close() the file afterwards, or use a with 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













2














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.






share|improve this answer





















  • 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











  • @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













  • @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
















2














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.






share|improve this answer





















  • 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











  • @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













  • @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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 indexing address[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 than adress = 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





    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













  • 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











  • 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



Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin