0

I have SQL table with Column values as (used only 1 record)

-l oracle -t employee.address -z 85248

When I extract data using CX_Oracle, I am getting below data that I am storing in a variable as sql_output as below (its tuple within list)

sql_output = [('-l oracle -t employee.address -z 85248',), 
 ('-l oracle -t employee.address -z 85249',),
 ('-l oracle -t employee.address -z 85220',),
 ('-l oracle -t employee.address -z 85215',),
 ('-l oracle -t employee.address -z 85240',),
 ('-l oracle -t employee.address -z 85258',)]

In above example -l, -t, -z are the parameters that I want to use as key I am looking for dictionary within list as so that I can loop over and run another command

desire_output = [{'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85248},
 {'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85249},
 {'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85220},
 {'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85215},
 {'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85240},
 {'l': 'oracle', 's': 'employee', 't': 'address', 'z': 85258}]

I have tried to convert this into string than a dictionary within list using as below

sql_output = [('-l oracle -t employee.address -z 85248',), 
('-l oracle -t employee.address -z 85249',),
('-l oracle -t employee.address -z 85220',),
('-l oracle -t employee.address -z 85215',),
('-l oracle -t employee.address -z 85240',),
('-l oracle -t employee.address -z 85258',)]
dic, lst = {}, []
for item in sql_output:
    itm = ' '.join(item).replace('-','').split(' ')
    dic[itm[0]]=itm[1]
    dic['s']=itm[3].split('.')[0]
    dic['t']=itm[3].split('.')[1]
    dic[itm[4]]=itm[5]
    print(dic)
    lst.append(dic)
print(lst)

result

[{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}, 
{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}, 
{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}, 
{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}, 
{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}, 
{'l': 'oracle', 's': 'employee', 't': 'address', 'z': '85258'}]

Not sure how to achieve this, my way seems to be super complicated, any solution?

Pavn
  • 160
  • 1
  • 10
  • 2
    What have you tried? This is not a hard problem. `split` and a few `if` statements would do it. – Tim Roberts Apr 26 '21 at 22:02
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Give me some code” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Apr 26 '21 at 22:05
  • Here's your answer : `s = [("-l oracle -t employee.address -z 85248",),] p = [] for sc in s: spl = sc[0].split(" ") p.append({spl[0][1]: spl[1], "s": spl[3].split(".")[0], spl[2][1]: spl[3][1], spl[4][1]: int(spl[5])}) print(p)` – Soroosh Noorzad Apr 26 '21 at 22:13
  • 1
    @Soroosh Please don't post answers in the comments. [Code formatting in comments is limited by design](https://meta.stackexchange.com/a/143309/343832). I would say post an answer instead, but the question is closed because it's too broad. We recommend you **answer well-asked questions**. See [answer] and [comment everywhere](/help/privileges/comment). – wjandrea Apr 26 '21 at 22:20
  • Hi @wjandrea . This user is absolutely a newbie. He needs help! – Soroosh Noorzad Apr 26 '21 at 22:23
  • @Soroosh Getting help here is more of a privilege than a right. This question is closed because this task has multiple steps and it's not clear what OP needs help with exactly. As well, it's downvoted because they don't seem to have put in any effort at solving the problem themselves. See [this explanation of why we close questions like this](https://meta.stackoverflow.com/q/337317/4518341) and [ask] for an idea of what a good question looks like. – wjandrea Apr 26 '21 at 22:33
  • Ok... Now I see what is the meaning of too broad... ok. So The questions with no efforts shall not get any answers? Is it true? @wjandrea – Soroosh Noorzad Apr 26 '21 at 22:37
  • 1
    @Soroosh Sort of. Questions about one specific task like for example "how do I convert a string to a number" don't really *need* to demonstrate any effort, but for a task like this with multiple steps, we want to see what OP has already tried and *where they've gotten stuck*, which lets us see exactly what the problem is and exactly what they need help with, which leads to higher-quality answers. – wjandrea Apr 26 '21 at 23:01
  • Does this answer your question? [How to convert strings into integers in Python?](https://stackoverflow.com/q/642154/4518341) – wjandrea Apr 27 '21 at 03:46
  • There are a lot of relatively minor things you could improve in your code, but that's too broad and getting out of scope for Stack Overflow. It'd be better to ask on [codereview.se], once you have the integer conversion working. – wjandrea Apr 27 '21 at 03:49
  • @SorooshNoorzad I thing you missed to add .split(".") between spl[3][1] Thanks I tried the longer way. What if we are not aware about the length of the elements ? – Pavn Apr 27 '21 at 03:51
  • It depends on each case of input. We can't structure something when we know nothing about it. There is always an answer to these types of questions. Don't worry about new cases and google them. Good luck @Pavn – Soroosh Noorzad Apr 27 '21 at 07:39
  • Oops, I missed that the numbers are all the same. See [Appending a dictionary to a list in a a loop Python](https://stackoverflow.com/q/23724136/4518341) – wjandrea Apr 27 '21 at 18:01

0 Answers0