-4

I have to strip() my output of the DB with Python and it already works. But is there a more elegant way to do this?

Here is my code:

import MySQLdb
conn= MySQLdb.connect("localhost","root","testPW","MixOMat")
c=conn.cursor()
c.execute("SELECT Zutat1 FROM Rezepte WHERE RezeptID=1")
z1=str(c.fetchall())
z1=z1.strip("(")
z1=z1.rstrip(")")
z1=z1.rstrip(",")
z1=z1.rstrip(")")
z1=z1.rstrip(",")
z1=z1.rstrip("L")
print(z1)
jofri
  • 131
  • 1
  • 16
  • 1
    Would `rstrip("),L").lstrip("(")` work for you? What exactly does the string look like before you alter it, and what do you need it to look like afterwards? – khelwood Mar 23 '17 at 12:14
  • 5
    show the initial `z1` value(before stripping) – RomanPerekhrest Mar 23 '17 at 12:15
  • As [the `str.strip` docs](https://docs.python.org/3/library/stdtypes.html#str.strip) say, "The _chars_ argument is not a prefix or suffix; rather, all combinations of its values are stripped" – PM 2Ring Mar 23 '17 at 12:17
  • 2
    This question lacks a proper example with the expected result after stripping. – timgeb Mar 23 '17 at 12:22
  • 2
    Why on earth are you converting the result to a string? Just extract out the field or fields you want. – Duncan Mar 23 '17 at 12:26
  • @Duncan That sounds sensible. I don't know `MySQLdb`, but I suspected this was an XY problem. Pity the OP couldn't show us some sample data... – PM 2Ring Mar 23 '17 at 12:32
  • So if I don't strip I get this Output: ((5L,),). But I need the 5 as a value to work with, later in the script. – jofri Mar 23 '17 at 12:33
  • `z1 = c.fetchone()[0]` will give you the 5. – Duncan Mar 23 '17 at 13:25
  • Okay, I do it without the: [0] and it works. For what is this 0? @Duncan – jofri Mar 23 '17 at 13:32
  • 1
    The `[0]` is to take the first element from the tuple that is returned. But whatever works for you, just don't convert to a string. – Duncan Mar 23 '17 at 13:48

3 Answers3

5

First, .strip parameter is the characters you want to remove, so you only need 2 calls

z1 = z1.strip('(').rstrip(')L')

Second, you're doing it wrong, the correct way to fetch your data should (I think) be:

c.execute("SELECT Zutat1 FROM Rezepte WHERE RezeptID=1")
(z1, ) = c.fetchone()

Note that I'm assuming you only have one row to retrieve

That will directly retrieve one integer instead of a string representation

Romuald Brunet
  • 5,595
  • 4
  • 38
  • 34
  • Okay, thank you. That makes it a lot easier. Now I can work with the long value without stripping – jofri Mar 23 '17 at 12:37
1

Just use a for loop

for e in ["(",")", ","...]:
    z1=z1.strip(e)

Functional aproach:

from functools import reduce

z1 = reduce(lambda x: z1.strip(x), ["(",")", ","...])

Or even just:

z1 = z1.strip("".join(["(",")", ","...]))
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • 2
    `"".join(["(",")", ","...])` Seriously? Why not just supply that arg as a literal? But anyway, this appears to be an XY problem, and the OP shouldn't be converting the object to a string & stripping it. – PM 2Ring Mar 23 '17 at 12:39
  • @PM2Ring, it doesnt really matter, but personally i like to separate to lists. anyway as you like. – Netwave Mar 23 '17 at 13:50
0
import MySQLdb
conn= MySQLdb.connect("localhost","root","notna99","MixOMat")
c=conn.cursor()
c.execute("SELECT Zutat1 FROM Rezepte WHERE RezeptID=1")
z1=str(c.fetchall())
z1=z1.strip("(").rstrip(")").rstrip(",").rstrip(")").rstrip(",").rstrip("L")
print(z1)

This is the another way to do it or you can use slicing.

Dev Jalla
  • 1,910
  • 2
  • 13
  • 21