Reputation: 21
I know there are plenty of titles that sound the same believe me I looked however this has to do with the quotes which make it a string which I have seen very little of and no solutions which worked for me.
I am trying to create a list which i can then put into a database there should normally be a couple hundred items to my list however the problem is the outside quotes turn the entire list into one big element and have had little luck trying to fix it
list with problem
list1 = ['("a"),("b"),("c"),("d"),("e")']
what I need
list1 = [("a"),("b"),("c"),("d"),("e")]
I have tried multiple things so far I have tried...
list1 = ['("a"),("b"),("c"),("d"),("e")']
listFormated = [x.replace("'", '') for x in list1]
instead of removing the outside quote from the list it took all quote marks from inside the string element and removed those instead. for example
list1= ['('a'),('b')('c')("let's")']
now becomes
list1= ['(a),(b),(c),("lets")']
Also Ive tried ast.literal_eval and it almost worked but take a look at the results
from ast import literal_eval
listFormated = [literal_eval(i) for i in list1]
but this returns
[('a','b','c',"let's")]
I would really appreciate any help someone could give.
EDIT:
I have not communicated correctly that I am trying to put bible text into a list in which each line is in a tuple so I can place it into an sqlite db so there wont just be single letters but multiple letters with apostrophes Collins and such and one of the answers were close but it splits each letter into a tuplized list and I need a way to keep the original formatting. Here is my first few lines to give a better example
['(" In the beginning God created the Heauen, and the Earth. "), (" And the ear th was without forme, and voyd, and darkenesse was vpon the face of the deepe: and the Spirit of God mooued vpon the face of the waters. "), (" And God said, Let there be light: and there was light. "), (" And God saw the light, that it was good: and God diuided the light from the darkenesse. ")']
Upvotes: 2
Views: 570
Reputation: 90
From your example, it looks like you have a list with one value, which is a string. Is that really what you want?
Otherwise, if you have the string '("a"),("b"),("c"),...'
, and want to convert it to a list, you could use the split
method:
stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
print(list_of_strings) # Will print ['("a")', '("b")', '("c")']
If you want to return only the strings, you could use ast.literal_eval
:
import ast
stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
list_of_letters: List[str] = [ast.literal_eval(letter) for letter in list_of_strings]
print(list_of_letters) # Will print ['a', 'b', 'c']
Or, if you would like to return tuples of the strings, you could cast the literal_eval(letter)
to tuple
list_of_tuples: List[Tuple[str]] = [tuple(ast.literal_eval(letter)) for letter in list_of_strings]
print(list_of_letters) # Will print [('a',), ('b',), ('c',)]
You can read more about ast.literal_eval
here
Quick note: If from the begging you had the squared brackets inside your string, you could have used ast.literal_eval
on the input:
import ast
stringified_list: str = '[("a"),("b"),("c")]'
list_of_strings: List[str] = ast.literal_eval(stringified_list)
print(list_of_strings) # Will print ['a', 'b', 'c']
Also, here is why you shouldn't use eval
directly.
There are two methods of solving it.
The first, you could loop over to list, and each time you find an opening parentheses you can start aggregating the following strings into some variable. Later, when you reach the closing parentheses, you can say that this sentence is finished and repeat the process with the next one.
txt = "('Fizz, Buzz!'), ('fizz buzz')"
sentances = []
last_sentance = ''
in_sentance = False
for t in txt:
if t == ')':
in_sentance = False
sentances.append(last_sentance)
last_sentance = ''
continue
if t == '(':
in_sentance = True
continue
if in_sentance:
last_sentance += t
print(sentences) # This will print ['Fizz, Buzz!', 'fizz buzz']
This isn't the best piece of code I wrote, but you get the idea.
Another method is to split using python regex:
import re
txt = "('Fizz, Buzz!'), ('fizz buzz')"
splitted = re.split("\(+(.*?)\)", txt,)
print(splitted) # This will print ['Fizz, Buzz!', ', ', 'fizz buzz']
You can than remove the commas using
filtered = list(filter((', ').__ne__, x))
print(filtered) # This will print ['Fizz, Buzz!', 'fizz buzz']
After that you can do the manipulations you want on this list, e.g. converting each item into a tuple etc.
Upvotes: 1
Reputation: 11
This should do what you're trying to do.
list1 = ['("a"),("b"),("c"),("d"),("e")']
list1 = [eval(list1[0])] # since the string is valid python, it can just be evalled
>>> list1
["a", "b", "c", "d", "e"]
Also, why does it contain a string in the first place? It seems like you'd rather have other data types, so why use strings?
Upvotes: 0
Reputation: 41
UPDATED
Is it good enough?
list1 = ['("a"),("b"),("c"),("d"),("e")']
list2 = list1[0].split(',')
list2
>> ['("a")', '("b")', '("c")', '("d")', '("e")']
If you actually need a list of single itemed tuples:
[tuple([x[2]],) for x in list1[0].split(',')]
>> [('a',), ('b',), ('c',), ('d',), ('e',)]
Note: As far as I understand, this is how Python shows tuples with single item in it
Upvotes: 2