Python Forum
Split the list and obtain a single value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split the list and obtain a single value
#1
Hi everyone,

I am gururaj deshpande (new user), i started using python recently for my college project purpose. My query is regarding SQL query parsing, i am using the open source API sqlparse to parse the given SQL query. The output I am getting is in the form of a tokenlist which looks somewhat like this
[<DML 'select' at 0x7FF7F1114E88>, <Whitespace ' ' at 0x7FF7F1114EF0>, <IdentifierList 'name,p...' at 0x7FF7F0D8A5D0>, <Whitespace ' ' at 0x7FF7F0D8D1F0>, <Keyword 'from' at 0x7FF7F1114E20>, <Whitespace ' ' at 0x7FF7F0D8D2C0>, <Identifier 'employ...' at 0x7FF7F0D8A4D0>, <Whitespace ' ' at 0x7FF7F0D8D390>, <Where 'where ...' at 0x7FF7F10CDED0>]
I want to extract only the single values from the obtained list for ex,only "select", "from" etc in the above given case.
Can anyone please help me with this issue? any suggestions are appreciated.

Thank you,
Guru
Reply
#2
I've never used sqlparse before. Nevertheless, exploring its source code might lead to the following solution, e.g.
import sqlparse
from sqlparse.tokens import Token  # look at source of tokens.py
raw = 'select * from foo; select * from bar;'
parsed = sqlparse.parse(raw)
list(filter(lambda x: x.ttype in [Token.Keyword, Token.Keyword.DML], parsed[0].tokens))
Output:
[<DML 'select' at 0x7F732037EEE8>, <Keyword 'from' at 0x7F732039F0A8>]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Resampling: How do I obtain the value of the last week of the month? JaneTan 2 973 Dec-12-2022, 12:49 AM
Last Post: JaneTan
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,454 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to split the input taken from user into a single character? mHosseinDS86 3 1,138 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,238 Jul-11-2022, 08:52 AM
Last Post: japo85
  Split single column to multiple columns SriRajesh 1 1,290 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,288 Apr-11-2021, 11:03 PM
Last Post: lastyle
  convert List with dictionaries to a single dictionary iamaghost 3 2,805 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,280 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Undo interation to make a single list? DustinKlent 2 2,133 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  unique (single) value in dict (or list) 3Pinter 5 2,404 Mar-27-2020, 12:55 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020