1

I am getting all values by scanning the tags

    list_values = []
    tags = ['Created', 'Comments', 'Name']
    for element in root.iter():
    if element.tag not in tags:
        continue
    print(element.text)
    list_values .append(element.text)

    print(list_values)

Getting all values in one array like ['12/2/2018','aa','John','13/2/2018','aa2','Tim','12/4/2018','aa','John','13/2/2018','gg','Kim']

Have to insert all these values in SQL. I want output like

  1. ('12/2/2018','aa','John')
  2. ('13/2/2018','aa2','Tim')
  3. (12/4/2018','aa','John')

2 Answers 2

2

Simply use the iter() as below:

data = ['12/2/2018','aa','John','13/2/2018','aa2','Tim','12/4/2018','aa','John','13/2/2018','gg','Kim']
it = iter(data)
print(list(zip(it,it,it)))

Output:

[('12/2/2018', 'aa', 'John'), ('13/2/2018', 'aa2', 'Tim'), ('12/4/2018', 'aa', 'John'), ('13/2/2018', 'gg', 'Kim')]
Sign up to request clarification or add additional context in comments.

1 Comment

Cool solution! ;)
1

try this:

data = ['12/2/2018','aa','John','13/2/2018','aa2','Tim','12/4/2018','aa','John','13/2/2018','gg','Kim']
[data[i:i + 3] for i in range(0,len(data),3)]

Output:

[['12/2/2018', 'aa', 'John'],
 ['13/2/2018', 'aa2', 'Tim'],
 ['12/4/2018', 'aa', 'John'],
 ['13/2/2018', 'gg', 'Kim']]

To convert to list of tuple use this:

data = ['12/2/2018','aa','John','13/2/2018','aa2','Tim','12/4/2018','aa','John','13/2/2018','gg','Kim']
[tuple(data[i:i + 3]) for i in range(0,len(data),3)]

4 Comments

Thanks a lot. Can you please let me know how can I insert this into the SQL database. How can I read this one by one or in one go I can Insert. Thanks again.
Depends on what database you are using, there should be some libraries that help you insert many at once
I am using SQL DB and import pyodbc. following this link, stackoverflow.com/questions/18244565/… . not able to figure out how can i read the above-genertaed array.
try the tuple way in my edited answer and see if that works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.