This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 4 years ago.
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?
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(':', '-')
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.
Related
This question already has answers here:
How to use digit separators for Python integer literals?
(4 answers)
Closed 2 years ago.
I seem to remember that there is a syntax in python to directly input numbers with comma separators (1,000,000 instead of 1000000). Googling the issue gives either results on how to:
Print numbers with comma separators
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
locale.format("%d", 1255000, grouping=True)
Remove commas from numbers to input into python
a = '1,000,000'
int(a.replace(',' , ''))
I don't want to do either and the plethora of results stops me from finding the information I need.
Instead of commas, Python allows the use of underscores.
See https://www.python.org/dev/peps/pep-0515/
grouping decimal numbers by thousands
amount = 10_000_000.0
grouping hexadecimal addresses by words
addr = 0xCAFE_F00D
grouping bits into nibbles in a binary literal
flags = 0b_0011_1111_0100_1110
same, for string conversions
flags = int('0b_1111_0000', 2)
This question already has answers here:
Understanding difference between Double Quote and Single Quote with __repr__()
(3 answers)
Closed 2 years ago.
I have recently started python and am having a frustratingly hard time trying to figure out how to print a raw string containing both ' and " characters. Even with various combinations of r'' and \ I cannot get python to return the literal string that I require.
Goal: Assign string of the following format to variable (similar format to be used as password). String: "1z8'aoVz1+9p}2C (16 character string starting with " and containing ').
Current result:
>>> string ='''"1z8'aoVz1+9p}2C'''
>>> string
'"1z8\'aoVz1+9p}2C'
>>> string =r'"1z8\'aoVz1+9p}2C'
>>> string
'"1z8\\\'aoVz1+9p}2C'
I have also tried splitting it into several variable and concatenating but python always returns the \ before '.
Thank you all for your help. I have learn a lot from this forum.
There is no need for raw string:
string =""""1z8'aoVz1+9p}2C"""
Now, if you do this:
>>> string =""""1z8'aoVz1+9p}2C"""
>>> string
you will get this:
'"1z8\'aoVz1+9p}2C'
but if you print it, you'll get what you want:
"1z8'aoVz1+9p}2C
This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 2 years ago.
I'm scraping some data from a website and using regex i was able to extract some strings in UTF-16 format. Using this site I'm able to decode the strings i extract but i want to do it all in Python.
The extracted text is in String format, not bytes. So a simple .encode() doesn't work.
For example:
String: \u0074\u0065\u0073\u0074 --> String: test
I can think of solving this by treating the string as a byte object, but i have no idea how to do this.
EDIT: The data chunk i've extracted from using regex:
I = new Array();
I[0] = new Array();
I[0][1] = new Array();
I[0][1][0] = new Array();
I[0][1][0][0] = '\u0074\u0065\u0073\u0074';
I[0][2]='';
Any help is appreciated.
Thanks
If you don't care about security, the simplest way is eval:
eval('"' + yourstringhere + '"')
But the correct way to do this would be:
x = bytes(yourstringhere, "utf-8").decode("unicode_escape")
the variable yourstringhere should contain the "String" object you mentioned before running this, obviously.
At first we convert the string to bytes using the bytes function. Then we decode using a special encoding called unicode_escape which parses the \u.... sequences into actual unicode characters
This question already has answers here:
Working with UTF-8 encoding in Python source [duplicate]
(2 answers)
How to output a utf-8 string list as it is in python?
(4 answers)
Closed 6 years ago.
While I managed to get all the data that I need as well as save it on a cv file, the output I get is in UTF-8 format, which is normal(correct me If I'm wrong)
TBH I've already "played" with the .encode() and .decode() option without any results.
here is my code
brands=[name.text for name in Unibrands]
here is the output
u'Spirulina \u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ae'
And this is the desired output
u'Spirulina Ελληνική'
That string is already fine; you're seeing the repr of it, which does escape certain characters because this is intended to be safe to copy and paste directly into Python source code (which in Python 2.x means it needs to have only printable ASCII characters) - eg, \u0395 represents the codepoint U+0395 GREEK CAPITAL LETTER EPSILON. You're seeing this form of it because printing a list (or other container) always shows you the repr of its contents - if you instead print the string directly, you should see an appropriate glyph instead of the escaped form:
>>> print(u'Spirulina \u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ae')
>>> 'Spirulina Ελληνική'
You could also consider upgrading to a newer Python version; Python 3.5 (and possibly earlier 3.x versions) no longer escape these letters in the repr, since Python now accepts Unicode characters in source files by default.
This question already has answers here:
Removing u in list
(8 answers)
Closed 6 years ago.
I want to remove 'u' from every element in the list, can anybody help me?
[u'four', u'gag', u'prefix', u'woods']
The issue is with the encoding of strings.
Do this :
l = [u'four', u'gag', u'prefix', u'woods']
l2 = [i.encode('UTF-8') for i in l]
print l2
['four', 'gag', 'prefix', 'woods']
The u is an attribute that tells what type of string it is. If it was a byte string, this would be b. If you call type on these, they will return String. The difference between Unicode and something like ASCII is that Unicode is a super-set of ASCII that is the same for 0-127, but has more capability to represent different types of characters. These can be UTF-8 or UTF-32 or whatever, but generally are larger than one byte.
It should behave the same for 99% of the things that you want to do, but you can also change the encoding if you have a function that needs a very particular type of string.