I have a list of tuples like so:
>>> all_names = c.execute("""select name from fb_friends""")
>>> for name in all_names:
... print(name)
('Jody Ann Elizabeth Lill',)
('Georgia Gee Smith',)
...(282 more)...
('Josh Firth',)
('Danny Hallas',)
And I want to create a table for each individual person. First I need to replace all spaces with an underscore in order to be SQLite3 table names, so I do that like so:
>>> all_names = c.execute("""select name from fb_friends""")
>>> for name in all_names:
... friends_name = name[0].replace(" ", "_")
... print(friends_name)
Jody_Ann_Elizabeth_Lill
Georgia_Gee_Smith
...(282 more)...
Josh_Firth
Danny_Hallas
So if I understand correctly I now have a list, not a list of tuples..? Which should be simple to create all my tables from this list like so:
>>> all_names = c.execute("""select name from fb_friends""")
>>> for name in all_names:
... friends_name = name[0].replace(" ", "_")
... c.execute("""create table {0} (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `work` TEXT NOT NULL, `education` TEXT, `current_city` TEXT, `phone_number` TEXT, `dob` TEXT, `gender` TEXT, `sexual_orientation` TEXT, `religion` TEXT, `relationship_status` TEXT, `about_me` TEXT )""".format(friends_name))
But all it does is create a table from the first name in the list, I would have thought the for
loop would iterate over the list of names and create a table for each one, which is what I want, can people please advise me on a few things:
- Is using the
.replace
method the best way to get the underscores in the names? If no then what is? - Why is the
for
loop not iterating over each name to create the tables? And how do I make it do that? - Are my methods correct at all? If not then what methods would do it better?