This question already has answers here:
How to convert string to Title Case in Python?
(10 answers)
Closed 9 years ago.
I'm having trouble trying to create a function that can do this job. The objective is to convert strings like
one to One
hello_world to HelloWorld
foo_bar_baz to FooBarBaz
I know that the proper way to do this is using re.sub, but I'm having trouble creating the right regular expressions to do the job.
You can try something like this:
>>> s = 'one'
>>> filter(str.isalnum, s.title())
'One'
>>>
>>> s = 'hello_world'
>>> filter(str.isalnum, s.title())
'HelloWorld'
>>>
>>> s = 'foo_bar_baz'
>>> filter(str.isalnum, s.title())
'FooBarBaz'
Relevant documentation:
str.title()
str.isalnum()
filter()
Found solution:
def uppercase(name):
return ''.join(x for x in name.title() if not x.isspace()).replace('_', '')
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Literally, I've been trying to a way to solve this but it seems that I'm poor on regex;)
I need to remove (WindowsPath and )"from the strings in a list
x= ["(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))", "(WindowsPath('D:/test/2_reptiles_bp.png'),WindowsPath('D:/test/2_crocodile_mp.png'))"]
So I tried
import re
cleaned_x = [re.sub("(?<=WindowsPath\(').*?(?='\))",'',a) for a in x]
outputs
["(WindowsPath(''),WindowsPath(''))", "(WindowsPath(''),WindowsPath(''))"]
what I need to have is;
cleaned_x= [('D:/test/1_birds_bp.png','D:/test/1_eagle_mp.png'), ('D:/test/2_reptiles_bp.png','D:/test/2_crocodile_mp.png')]
basically tuples in a list.
You can accomplish this by using re.findall like this:
>>> cleaned_x = [tuple(re.findall(r"[A-Z]:/[^']+", a)) for a in x]
>>> cleaned_x
[('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png'), ('D:/test/2_reptiles_bp.png',
'D:/test/2_crocodile_mp.png')]
>>>
Hope it helps.
Perhaps you could use capturing groups? For instance:
import re
re_winpath = re.compile(r'^\(WindowsPath\(\'(.*)\'\)\,WindowsPath\(\'(.*)\'\)\)$')
def extract_pair(s):
m = re_winpath.match(s)
if m is None:
raise ValueError(f"cannot extract pair from string: {s}")
return m.groups()
pairs = list(map(extract_pair, x))
Here's my take,
not pretty, and I did it in two steps so as not to make regexp spagetti, and you could turn it into a list comprehension if you like, but it should work
for a in x:
a = re.sub('(\()?WindowsPath', '', a)
a = re.sub('\)$','', a)
print(a)
This question already has answers here:
Dot notation string manipulation
(5 answers)
Closed 6 years ago.
I have a column with data like this that I'm accessing via Python:
501,555,570=3.5
I want to get 570=3.5.
How can I do that? Would it be a variation of the split command?
You can use the str.rsplit() function as follows:
In [34]: s = '501,555,570=3.5'
In [35]: s.rsplit(',', 1)[1]
Out[35]: '570=3.5'
>>> s = '501,555,570=3.5'
>>> s.split(",")[-1]
'570=3.5'
This will access the last element in the split string. It is not dependent on how many commas are in the string.
Example of longer string:
>>> s = "501,555,670,450,1,12,570=3.5"
>>> s.split(",")[-1]
'570=3.5'
A slight variation of wim's answer, if you're in a recent Python 3 version:
>>> s = '501,555,570=3.5'
>>> *others, last = s.split(',')
>>> last
'570=3.5'
>>> s = '501,555,570=3.5'
>>> others, last = s.rsplit(',', 1)
>>> last
'570=3.5'
Another variation, and how I would do it myself:
>>> s = '501,555,570=3.5'
>>> last = s.split(',')[-1]
>>> last
'570=3.5'
Using rpartition and as # MartijnPieters♦ mentioned here in his comment.
for a single split, str.rpartition() is going to be faster.
>>> s.rpartition(',')[-1]
'570=3.5'
You could take substring from the position after where you find the last comma.
s[s.rfind(',') + 1 : ]
This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 8 years ago.
I have a file which contains each line in the following format
"('-1259656819525938837', 598679497)\t0.036787946" # "\t" within the string is the tab sign
I need to get the components out
-1259656819525938837 #string, it is the content within ' '
598679497 # long
0.036787946 # float
Python 2.6
You can use regular expressions from re module:
import re
s = "('-1259656819525938837', 598679497)\t0.036787946"
re.findall(r'[-+]?[0-9]*\.?[0-9]+', s)
% gives: ['-1259656819525938837', '598679497', '0.036787946']
"2.7.0_bf4fda703454".split("_") gives a list of strings:
In [1]: "2.7.0_bf4fda703454".split("_")
Out[1]: ['2.7.0', 'bf4fda703454']
This splits the string at every underscore. If you want it to stop after the first split, use "2.7.0_bf4fda703454".split("_", 1).
If you know for a fact that the string contains an underscore, you can even unpack the LHS and RHS into separate variables:
In [8]: lhs, rhs = "2.7.0_bf4fda703454".split("_", 1)
In [9]: lhs
Out[9]: '2.7.0'
In [10]: rhs
Out[10]: 'bf4fda703454'
You can use a regex to extract number and float from string:
>>> import re
>>> a = "('-1259656819525938837', 598679497)\t0.036787946"
>>> re.findall(r'[-?\d\.\d]+', a)
['-1259656819525938837', '598679497', '0.036787946']
This question already has answers here:
Split a string to even sized chunks
(9 answers)
Closed 8 years ago.
how can I separate a string: "Blahblahblahblah" into "Blah" "blah" "blah" "blah" on python. I've tried the following:
str = "Blahblahblahblah"
for letter[0:3] on str
How can I do it?
If you do not mind to use re library. In this example the regex .{4} means any character except \n of length 4.
import re
str = "Blahblahblahblah"
print re.findall(".{4}", str)
output:
['Blah', 'blah', 'blah', 'blah']
Note: str is not a very good name for a variable name. Because there is a function named str() in python that converts the given variable into a string.
Try:
>>> SUBSTR_LEN = 4
>>> string = "bla1bla2bla3bla4"
>>> [string[n:n + SUBSTR_LEN] for n in range(0, len(string), SUBSTR_LEN)]
['bla1', 'bla2', 'bla3', 'bla4']
This question already has answers here:
Python Trailing L Problem
(5 answers)
Closed 9 years ago.
I receive from a module a string that is a representation of an long int
>>> str = hex(5L)
>>> str
'0x5L'
What I now want is to convert the string str back to a number (integer)
int(str,16) does not work because of the L.
Is there a way to do this without stripping the last L out of the string? Because it is also possible that the string contains a hex without the L ?
Use str.rstrip; It works for both cases:
>>> int('0x5L'.rstrip('L'),16)
5
>>> int('0x5'.rstrip('L'),16)
5
Or generate the string this way:
>>> s = '{:#x}'.format(5L) # remove '#' if you don' want '0x'
>>> s
'0x5'
>>> int(s, 16)
5
You could even just use:
>>> str = hex(5L)
>>> long(str,16)
5L
>>> int(long(str,16))
5
>>>