Replace last occurrence only in a string [duplicate] - python

This question already has answers here:
rreplace - How to replace the last occurrence of an expression in a string?
(8 answers)
Closed 4 years ago.
I have the following string:
s = 'JSON, XML, XLS, XLSX, CSV, TSV'
I would like to convert it to have an "and" at the end. Is it possible to do a replace operation only at the last occurrence of something, such as:
s.replace(', ', ', and ', -1)
==>
'JSON, XML, XLS, XLSX, CSV, and TSV'

s = 'JSON, XML, XLS, XLSX, CSV, TSV'
s_spl = s.split()
s_new = " ".join(s_spl[:-2]) + ' and ' + s_spl[-1]
print(s_new)

Related

Strip a single quotation ( ' ) from a list from CSV [duplicate]

This question already has answers here:
How do I escape backslash and single quote or double quote in Python? [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to print out a list and strip the " ' " (single quotes) character from a list I generated from a CSV File.
Here's the code I have
import numpy as np
import pandas as pd
export = pd.read_csv('File') #open file
skuList = export.values.T[0].tolist() #transpose DF + convert to list
#print(skuList)
skuList = [value.strip(" ' ") for value in skuList] #strip ' ' '
print(skuList)
The output from this program is
'GL2i-RS-36', '523-30', 'RK623-30', ....
The output that I would want would be:
GL2i-RS-36, 523-30, RK623-30, ....
Is there a way to print out a list without the single quotes?
Maybe try this (without the spaces around the '):
skuList = [value.strip("'") for value in skuList] #strip ' ' '

How to delete certain words from string without spaces? [duplicate]

This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')

How to remove " " from text using regex-Python 3 [duplicate]

This question already has answers here:
Remove quotes from String in Python
(8 answers)
Closed 3 years ago.
So, I have a text string that I want to remove the "" from.
Here is my text string:
string= 'Sample this is a string text with "ut" '
Here is the output I want once using a regex expression:
string= 'Sample this is a string text with ut'
Here is my overall code:
import re
string= 'Sample this is a string text with "ut" '
re.sub('" "', '', string)
And the output just show the exact text in the string without any changes. Any suggestions?
You can simply use string.replace('"','')
If you want just remove all " symbols, you can use str.replace instead:
string = string.replace('"', '')

How do I move the second word BEFORE the first? [duplicate]

This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))

Python - How to ignore white spaces in double quotes while splitting a string? [duplicate]

This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 9 years ago.
I have my data as below
string = ' streptococcus 7120 "File being analysed" rd873 '
I tried to split the line using n=string.split() which gives the below result:
[streptococcus,7120,File,being,analysed,rd873]
I would like to split the string ignoring white spaces in " "
# output expected :
[streptococcus,7120,File being analysed,rd873]
Use re.findall with a suitable regex. I'm not sure what your error cases look like (what if there are an odd number of quotes?), but:
filter(None, it.chain(*re.findall(r'"([^"]*?)"|(\S+)', ' streptococcus 7120 "File being analysed" rd873 "hello!" hi')))
> ['streptococcus',
'7120',
'File being analysed',
'rd873',
'hello!',
'hi']
looks right.
You want shlex.split, which gives you the behavior you want with the quotes.
import shlex
string = ' streptococcus 7120 "File being analysed" rd873 '
items = shlex.split(string)
This won't strip extra spaces embedded in the strings, but you can do that with a list comprehension:
items = [" ".join(x.split()) for x in shlex.split(string)]
Look, ma, no regex!

Categories