how to remove u' and ' from tuple [duplicate] - python

This question already has answers here:
Remove 'u' from a python list
(5 answers)
Closed 5 years ago.
i saw that this question already been asked but the solutions i saw didn't worked for me.
i have a python script with tuple list which i want to retrieve without the unicode char (u') and without any other chars (like < [] > or < ' >) so only the data will pass to parameter.
my code look like this -
sql_cursor = con.cursor()
cursor = con.cursor()
cursor.execute(get_alerted_ip)
Results = cursor.fetchall()
for row in Results:
ip_to_open.append(row)
print (row)
con.close()
the output is -
(u'172.1.1.124',)
and the wanted output should look like -
172.1.1.124
I have already tried to convert the tuple to list or string in order to use methods like replace but it's not working.
what am i doing wrong? please help.

Simply
row[0].encode("utf-8")
will return the ascii version of the string

Related

How to exclude words from string [duplicate]

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("/"," ")

convert list into string? [duplicate]

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']).

How do I import a series of String data into a list, without manually putting the " " sign to each item added to the list? [duplicate]

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)

Replacing integers with variables in SQL commands in python [duplicate]

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+';)'

Python - concatenate a string to include a single backslash [duplicate]

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'

Categories