why do i get these results using rstrip()? [duplicate] - python

This question already has answers here:
Remove substring only at the end of string [duplicate]
(11 answers)
Closed 4 years ago.
I want to keep the file names without the.csv extension, but using rstrip('.csv') deletes the last letter in the strings ending in s:
data_files = [
"ap_2010.csv",
"class_size.csv",
"demographics.csv",
"graduation.csv",
"hs_directory.csv",
"sat_results.csv"
]
data_names = [name.rstrip('.csv') for name in data_files]
I get this results:
["ap_2010", "class_size", "demographic","graduation","hs_directory", "sat_result"]
The end s of strings demographics and sat_results has been removed, why does this happen??

This is because rstrip() strips all characters separately from the end of your string.
>>> 'abcdxyx'.rstrip('yx')
'abcd'
This will search for y and x to strip from the right side of your string. If you like to remove the .csv you can use split instead.
>>> "ap_2010.csv".split('.')[0]
"ap_2010"
Also for Filenames it is good practice to use the function os.path.splitext:
>>> import os
>>> os.path.splitext('ap_2010.csv')[0]
"ap_2010"

You can get your intended output with this:
data_files = [
"ap_2010.csv",
"class_size.csv",
"demographics.csv",
"graduation.csv",
"hs_directory.csv",
"sat_results.csv"
]
data_names = [name.replace('.csv','') for name in data_files]

Related

How to split my string "a!b!" into a!, b! in python? [duplicate]

This question already has answers here:
Python split() without removing the delimiter [duplicate]
(4 answers)
Closed 11 months ago.
Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.
How about :
string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)
Output :
['a!', 'ab!', 'b!']
split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this
s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]
# output : ["a!", "b!"]

lstrip and rstrip not removing in python 3? [duplicate]

This question already has answers here:
How do the .strip/.rstrip/.lstrip string methods work in Python?
(4 answers)
Closed 1 year ago.
I feel like I have a very straightforward piece of code. I have a file name that follows the form of 'stuff_category.csv'. I'm trying to remove 'stuff_' and '.csv', so that I will be left with 'category', which I need for the next piece of code. 'stuff_' can be many different things, so I can't use the replace() function. Right now I have
filename = "stuff_category.csv"
category = filename.lstrip('_').rstrip('.')
But if I try print(category), or even print(category.lstrip('_')), it just returns the original file name. What am I missing?
You could do it using removeprefix and removesuffix (Python 3.9) rather than lstrip and rstrip:
filename = "stuff_category.csv"
print(filename.removeprefix('stuff_').removesuffix('.csv')) #category
Or using Slicing with startswith endswith (Python 3.8):
start = "stuff_"
end = ".csv"
if filename.startswith(start):
filename = filename[len(start):]
if filename.endswith(end):
filename = filename[:-len(end)]
print(filename) #category
Or even use Slicing with just index:
print(filename[filename.index('_')+1:filename.index('.')]) #category
You're missing the documentation for these methods. They don't use the provided character as a delimiter, they remove the characters as if a substring.
lstrip(self, chars=None, /)
Return a copy of the string with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
Try this:
filename = "stuff_category.csv"
category = filename.lstrip('stuff_').rstrip('.csv')
Consider using a regular expression or str.split instead of lstrip/rstrip if "stuff_" isn't constant.

How to get a string after and before a specific substring in Python? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]

Add a single backslash ("\") to string in python [duplicate]

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I have an array of strings which looks like:
["U0001f308", "U0001F602"]
I need to add “\” in front of the first letter U so the output will be like:
["\U0001f308", "\U0001F602"]
This is the code I have tried so far:
matches = ["U0001f308", "U0001F602"]
emojis = [emoji.replace('U', r"\U") for emoji in matches]
print(emojis) #this prints ['\\U0001f308', '\\U0001F602'] which has two blacklashes
How can i add only one backslash in front of every string?
I guess what you want is the following code:
matches = ["U0001f308", "U0001F602"]
emojis = [emoji.replace('U', r"\U").encode().decode('unicode-escape') for emoji in matches]
print(emojis)
which prints
['🌈', '😂']
It's the same result as when we execute the following code:
print(["\U0001f308", "\U0001F602"])

Finding strings with gaps that match a string in a list of strings [duplicate]

This question already has answers here:
Python wildcard search in string
(6 answers)
Closed 5 years ago.
I have a list of strings that looks like this: ['ban*', 'c*rr*r', 'pl*s', pist*l ]. I want to check if those strings have matching equivalents in another list of strings which is the following:
['banner', 'bannana', ban, 'carrer', 'clorror', 'planes', 'plots']
Comparing first string from the list I have'banner' and 'bannana' and that would mean that there is a word that is matching that string ("ban*") So the '*' means that there can be one or more letters in that word.
Try this fnmatch approach
import fnmatch
lst = ['banner', 'bannana', 'ban', 'carrer', 'clorror', 'planes', 'plots']
f1 = fnmatch.filter(lst, 'ban*')
print (f1)
Output
['banner', 'bannana', 'ban']

Categories