This question already has answers here:
How to remove specific substrings from a set of strings in Python? [duplicate]
(9 answers)
Closed 1 year ago.
I'm using Python to get title names for a header in a program.
Each title comes like: "OXD/Overview Controls", "OXD/BULK/BULK Details Controls" and more...
Currently I am using a value/split() method to remove the "/" returning only "OXD Overview Controls" and "OXD BULK", however I would like to remove the "OXD" and "Controls" portions of the string. Any help is appreciated, my current code is listed below.
if value <> None:
value = value.split("/")[:2]
value = ' '.join(value)
else:
value = ""
return value.upper()
#Steven Barnard and #DSteman: Sorry, I wrote something and you both was faster as me. ^^'
You can use the string_variable.replace(search,replace_with) function.
Replace OXD with nothing:
value.replace("OXD","")
Replace / with Space:
value.replace("/"," ")
This question already has answers here:
How to convert list to string [duplicate]
(3 answers)
Closed 2 years ago.
Here is my list ['k:1','d:2','k:3','z:0'] now I want to remove apostrophes from list item and store it in the string form like 'k:1 , d:2, k:3, z:0' Here is my code
nlist = ['k:1','d:2','k:3','z:0']
newlist = []
for x in nlist:
kk = x.strip("'")
newlist.append(kk)
This code still give me the same thing
Just do this : print(', '.join(['k:1','d:2','k:3','z:0']))
if you want to see them without the apostrophes, try to print one of them alone.
try this:
print(nlist[0])
output: k:1
you can see that apostrophes because it's inside a list, when you call the value alone the text comes clean.
I would recommend studying more about strings, it's very fundamental to know how they work.
The parenthesis comes from the way of representing a list, to know wether an element is a string or not, quotes are used
print(['aStr', False, 5]) # ['aStr', False, 5]
To pass from ['k:1','d:2','k:3','z:0'] to k:1 , d:2, k:3, z:0 you need to join the elements.
values = ['k:1','d:2','k:3','z:0']
value = ", ".join(values)
print(value) # k:1, d:2, k:3, z:0
What you have is a list of strings and you want to join them into a single string.
This can be done with ", ".join(['k:1','d:2','k:3','z:0']).
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I'm a newbie in python.
Basically what I wanted to achieve is:
I have a block of sample data as shown here:
384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180
How do I code using python3 to achieve the following without having to manually to add the " " sign for each item after the comma.
The desired outcome:
datalist = list()
print(datalist)
Results:
['stritem1','stritem2','stritem3','stritem4'....etc]
Something like this should work:
rawData = "384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180"
datalist = rawData.split(',')
print(datalist)
This question already has answers here:
Escaping chars in Python and sqlite
(4 answers)
Closed 4 years ago.
So currently my code is this which works
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID="1";')
But as others use my programme the customer will change so how do I replace 1 with a variable like this..
cat = str(1)
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID=cat;')
However, this doesn't work so any help thanks
Have you considered breaking the SQL statement.
cat = '\"+'str(1)+'\"';
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID='+cat+';)'
This question already has answers here:
What is the difference between __str__ and __repr__?
(28 answers)
Closed 7 months ago.
In Python 2.6, I need to create a string by concatenating INTRANET\ and a userid such as jDoe to obtain a string INTRANET\jDoe. This will string will be a part of a SQL query. I have tried this a number of ways but end up getting INTRANET\\jDoe and hence my query does not return any results.
I want to do this:
a = 'INTRANET\\'
b = 'jDoe'
c = a+b ### want to get c as 'INTRANET\jDoe', not 'INTRANET\\jDoe'
Thanks
The problem seems a little different:
When I print c, I get 'INTRANET\jDoe'. But when I append c to a list (to be used in a sql query) as below:
list1 = []
list1.append(c)
print list1
>>>['INTRANET\\jDoe']
Why is this ?
The additional \ is there due to python escaping.
>>> print 'INTERNET\\jDoe'
INTERNET\jDoe
It doesn't affect the SQL you are using. You should look at another direction.
Try the following code,
s1 = "INTRANET"
s1 = s1 + "\\"
s1 = s1 + "jDoe"
print s1
This will give the correct output INTERNET\jDoe
If you simply try to view the content of the variable you will see an extra \, which is an escape sequence in python. In that case it will show,
'INTERNET\\jDoe'