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.
Related
This question already has answers here:
Index-wise Replacement of String Data in Python
(1 answer)
String replace in Python, in sequence, by index [duplicate]
(1 answer)
Closed 1 year ago.
I'm a newbie who just Started learning Python from YouTube. I am trying to make a program to replace old string Numbers with new string Numbers and facing problems while replacing numbers. Want to replace index-wise (What is its technical term (I don't know)). It can go in one direction or index-wise.
my string is = (001001001001001001001001001001001001001101100100110110011011001101011010011010110011011)
and I want to replace 101 with 01, 1101 with 11, 1001 with 011, and 11001 with 111,
so my replaced string/output string will be like this..
(00011000110001100011000110001100110110011011010110101100110111011)
As per python's normal string replace method it Cant work Anyone can help my
string = "001001001001001001001001001001001001001101100100110110011011001101011010011010110011011"
string = string.replace('101', '01').replace('1101', '11').replace('1001', '011').replace('11001', '111')
fin.close()
fin = open("2x.txt", "wt")
fin.write(string)
fin.close()
(00011000110001100011000110001100110110011011010110101100110111011)
In general python you can't "edit" strings, you need to create new ones. E.g:
my_old_string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
# use .replace()
my_new_string = my_old_string.replace('010', '0')
You could achieve the same thing with a single variable aswell:
string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
string = string.replace('010', '0')
string = string.replace('1101', '11')
# continue with this as often as you want
I am not sure, if your "doing all in one line" syntax is valid
This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Closed 2 years ago.
is there a function in python that does something like this:
input:
text = "s.om/e br%0oken tex!t".remove(".","/","%","0","!")
print(text)
output:
some broken text
The only thing that i know that can kinda to this is .replace("x", "") and that takes way too long to get rid of lots of different charicters. Thanks in advance.
Use regex module re to replace them all. The [] means any character in it :
text = re.sub("[./%0!]", "", "s.om/e br%0oken tex!t")
There is a module named re which is used in Regular expressions. You can use its sub function to replace or substitute characters from a string. Then you can try like this:
from re import sub
text = sub("[./%0!]","","The string")
print(text)
Regex details: Character class of . / % 0 ! if these are found in string replace them with a blank string and later print the text variable.
You might use str.maketrans combined with .translate; example:
t = str.maketrans("","","./%0!")
text = "s.om/e br%0oken tex!t"
cleantext = text.translate(t)
print(cleantext) # print(cleantext)
maketrans accept 3 arguments, every n-th character from first will be replaced with n-th character from second, all characters present in third will be jettisoned. In this case we only want to jettison so 1st and 2nd arguments are empty strs.
Alternatively you might use comprehension as follows:
text = "s.om/e br%0oken tex!t"
cleantext = ''.join(i for i in text if i not in "./%0!")
print(cleantext) # some broken text
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 2 years ago.
I am kind of noob in python and struck in middle of code. I want to trim my string.
For example- my string is "bangalore store 1321" and i want to trim it to "banglore"
Looks like you want to keep the first word (which is not "trimming" though). So you do two things
break the string into a list of words (where "word" is something separated by spaces)
take the first element of that list
words = mystring.split(' ')
result = words[0]
For a slicing answer:
def sub_string(str, start,end):
return str[start:end]
You can also use split, by definition, this splits by spaces, if any other delimiter needed, you can identity it inside the split arguments split(',')
def split_string(str):
return str.split()
This function will return an array of strings. Choose whichever you want from this array
str="bangalore store 1321"
print(str.split(' ')[0])
Output
bangalore
You can use str's partition method to avoid creating a list like str.split
>>> first_word, _, _ = s.partition(' ') # _ is a convention for a throwaway variable
>>> print(first_word)
bangalore
str.partition takes one argument - the separator - and returns the parts of the string before and after the first occurrence of the separator.
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]
This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 5 years ago.
If given the string "John Doe;Lodging;123;050617", which is a line within a file, I need to remove everything before and including the first semicolon, and after and including the last one. How can I do that without removing my ability to later split the remaining substring?
Previous questions do not address removing the portions once separated while retaining the portion needed for further use.
Stepwise for clarity:
string = "John Doe;Lodging;123;050617"
lst = string.split(';')
lst = lst[1:-1]
string = ';'.join(lst)
print(string)
>>> 'Lodging;123'
As one line:
';'.join('John Doe;Lodging;123;050617'.split(';')[1:-1])
>>> 'Lodging;123'
s = "John Doe;Lodging;123;050617"
new_s = ';'.join(s.split(';')[1:-1])
This will make new_s = 'Lodging;123'
test = "John Doe;Lodging;123;050617"
';'.join(test.split(';')[1:-1])
The find method will give you the index of the first instance of a character in a string
original_s="John Doe;Lodging;123;050617"
idx = original_s.find(';')
new_s = original_s[i+1:]
print(new_s)
There are a few methods you could try. The simplest one is to split the string and reassemble it:
data = 'John Doe;Lodging;123;050617'
';'.join(data.split(';')[1:-1])
You could also use find and rfind (like find, but from the end of the string backwards:
data[data.find(';')+1:data.rfind(';')]
There's also partition and rpartition:
first, sep, remainder = data.partition(';')
middle, sep, final = remainder.rpartition(';')
Use str.partition and str.rpartition.
s = "John Doe;Lodging;123;050617"
s.partition(';')[-1].rpartition(';')[0] # Lodging;123
In terms of efficiency, this is probably the best solution. However, I find the split/join method more readable.
Try:
s = "John Doe;Lodging;123;050617"
print(';'.join(s.split(';')[1:-1]))
Try this: use str.find() function to locate the first occurance of ';' and then substring from that point to the end using [:]
aStr = "John Doe;Lodging;123;050617"
aStr = aStr[aStr.find(';')+1:]
print(aStr)
Lodging;123;050617