0

I'm very knew to programming. During my course in university we have to work with spyder writing python, but mostly using sql. I am struggling a lot with a problem where transfering a list to a DataBase it transforms it into string, and when i wish to use it all comes in string, so for example list[0]=[ . I've scowered the Internet for answers and tried everything but i can't seem to find an answer. Well actually, i haven't tried doing a for cycle, but i think that would make the function even more confusing for me, at least.

And here's the function i've been working on, the line with the r is just me messing with to see if i can find a solution

def GRAFICO(nomeBD, codigo):
    bd=sql.connect(nomeBD, isolation_level=None)
    com='SELECT IdV, Cargas, Deflecao, Comp, ASeccaoA, ASeccaoB, IdMV FROM Ensaios, Vigas WHERE IdV=IdE AND IdMV='+'"'+codigo+'"'+';'
    res= bd.execute(com).fetchall()
    n=len(list(res[0][1]))
    m=len(res)
    M=res[0][6]
    E_total=[]
    Id_total=''
    w_total=''
    for r in range(m+1):
        Id_total=Id_total+res[r][0]
        print(Id_total)
        w_total=w_total+res[r][1]
        print(w_total)
        for i in range(n+1):      
            w=res[r][1][i]
            r= [int(word) for word in w.split() if word.isdigit()]
            print(r)
            delta=res[r][2][i]
            L=res[r][3]
            a=res[r][4]
            b=res[r][5]
            E=equacao(w, delta, L, a, b)
            E_total.append(E)
    Media=media(E_total)
    plot1(w_total, E_total, Id_total, Media, M)

The problem is i get a list like this w=['101', '109', '118', '128', '141', '157', '176'], but if i do list(w), i get this: ['[', "'", '1', '0', '1', "'", ',', ' ', "'", '1', '0', '9', "'", ',', ' ', "'", '1', '1', '8', "'", ',', ' ', "'", '1', '2', '8', "'", ',', ' ', "'", '1', '4', '1', "'", ',', ' ', "'", '1', '5', '7', "'", ',', ' ', "'", '1', '7', '6', "'", ']']. It's all in string, I'd like to be able to do w[0] and the output be 101, not '['

If you could help me it would be very much apreciated

  • 2
    Please remove all irrelevant code that does not match the question in the title. Make a small, self contained example that starts with a string and shows what your desired result is. – timgeb May 22 '20 at 12:35
  • what would be the irrelevant code, loading the data base? Thanks for helping! – Vasco Santos May 22 '20 at 12:38
  • 1
    Your question is how to transform a string with numerals into a list. Why are we talking about databases? – timgeb May 22 '20 at 12:39
  • Did you forget quotes around the value of `w`? Right now it is already a list, not a string. – timgeb May 22 '20 at 12:44
  • You are right, that's really not as important. However as I'm a bit lost maybe there could be a way to retrieve from the data base and not have the list in strings. But the main issue is really the i put a list into a DB and now that i retrieve it to use it i can't seem to find a way to transfer the number into integer and in a list again – Vasco Santos May 22 '20 at 12:46
  • `w = [int(num) for num in w]` this will convert string number to integer – deadshot May 22 '20 at 12:47
  • Check out [`literal_eval`](https://docs.python.org/3.9/library/ast.html#ast.literal_eval). And make use of the `int` builtin if you want to convert strings like `'101'` to ints. – timgeb May 22 '20 at 12:47
  • @komatiraju032 `w` is a string most likely. – timgeb May 22 '20 at 12:48
  • That's the thing!!! When i print(w) it comes of as what appears to be a list( like this:['101', '109', '118', '128', '141', '157', '176']), but if i print w[0] the output is [ – Vasco Santos May 22 '20 at 12:48
  • 1
    `[int(x) for x in ast.literal_eval(w)]`. (I can't tell from your code if there's a better way of fixing the issue upstream.) – timgeb May 22 '20 at 12:49
  • I got this error, can you help me make something of it? File "E:\anaconda\lib\ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "", line 1 [ ^ SyntaxError: unexpected EOF while parsing – Vasco Santos May 22 '20 at 12:52
  • Looks like your actual strings look different from the `w` you posted. This is why we need [MREs](https://stackoverflow.com/help/minimal-reproducible-example) so desperately around here. – timgeb May 22 '20 at 12:54
  • Btw @timgeb thx so much for the help! – Vasco Santos May 22 '20 at 12:54
  • 1
    You are right, I understand I'm not being the clearest in regards to explaining my problem. I'm going to see if I can find a way to explain myself better – Vasco Santos May 22 '20 at 12:56
  • you can use regex it will work – deadshot May 22 '20 at 12:57
  • But that's the thing i dont understand, how come when i print w I get this output: ['101', '109', '118', '128', '141', '157', '176'] but if i print list(w) i get this: ['[', "'", '1', '0', '1', "'", ',', ' ', "'", '1', '0', '9', "'", ',', ' ', "'", '1', '1', '8', "'", ',', ' ', "'", '1', '2', '8', "'", ',', ' ', "'", '1', '4', '1', "'", ',', ' ', "'", '1', '5', '7', "'", ',', ' ', "'", '1', '7', '6', "'", ']'] – Vasco Santos May 22 '20 at 12:58
  • @komatiraju032 can you show me a bit of how i use regex please? – Vasco Santos May 22 '20 at 13:01
  • use this `w = [int(num) for num in re.findall('\d+', w)]` – deadshot May 22 '20 at 13:36
  • @komatiraju032 how can i use so that floats mantain intact? For example: [int(num) for num in re.findall('\d+',[15.26]] = [15,26] – Vasco Santos May 22 '20 at 14:56
  • This will help https://stackoverflow.com/a/26137982/9050514 – deadshot May 22 '20 at 15:47

1 Answers1

0

You can use map(int,w) to create a list of integers from the given list in python 2.x

If you are using python 3.x use list(map(int,w)) to convert the lazy iterator to a list.