Bottom Page

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
Quote
#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>]
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Counting number of occurrences of a single digit in a list python_newbie09 12 297 Aug-12-2019, 01:31 PM
Last Post: perfringo
  [split] Automate the boring stuff, inserting commas in list srikanth 1 137 Jul-02-2019, 02:29 PM
Last Post: metulburr
  How to take a single value from dictionary ginjaninja247 1 125 May-28-2019, 12:34 PM
Last Post: buran
  Split List and Sublist from Pyodbc parthi1705 1 248 May-05-2019, 10:44 AM
Last Post: Larz60+
  merging sublist into single list in python abhishek8singhai 8 490 Mar-22-2019, 11:46 PM
Last Post: micseydel
  How to obtain the background color of a cell using xlrd? Krszt 1 1,079 Mar-12-2019, 11:23 PM
Last Post: hshivaraj
  Asking for help in solving a single variable nonlinear equation using Python ! NDP 0 190 Feb-15-2019, 12:03 PM
Last Post: NDP
  String being broken up into single characters ammorgan 1 296 Dec-31-2018, 07:06 AM
Last Post: Gribouillis
  SyntaxError: multiple statements found while compiling a single statement DragonG 1 711 Nov-26-2018, 05:33 AM
Last Post: Larz60+
  Python-for loop print into single line dragan979 4 520 Nov-23-2018, 01:01 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)