How to remove string quotes from list in Python - python

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. ")']

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

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.
After edit
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.

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?

Related

Python: Replace single character with multiple values

I have a string with "?" as placeholders. I need to loop through the string and replace each "?" with the next value in a list.
For example:
my_str = "Take the source data from ? and pass to ?;"
params = ['temp_table', 'destination_table']
Here's what I've tried, which works:
params_counter = 0
for letter in my_str:
if letter == '?':
# Overwrite my_str with a new version, where the first ? is replaced
my_str = my_str.replace('?', params[params_counter], 1)
params_counter += 1
The problem is it seems rather slow to loop through every single letter in the string, multiple times. My example is a simple string but real situations could be strings that are much longer.
What are more elegant or efficient ways to accomplish this?
I saw this question, which is relatively similar, but they're using dictionaries and are replacing multiple values with multiple values, rather than my case which is replacing one value with multiple values.
you don't need to iterate, replace will replace the first occcurrence of the string:
a clever solution is to split by the string you are searching, then zipping the list with a list of replacements of length the same and then joining
list1 = my_str.split('?')
params = ['temp_table', 'destination_table']
zipped = zip(list1, params)
replaced = ''.join([elt for sublist in zipped for elt in sublist])
In [19]: replaced
Out[19]: 'Take the source data from temp_table and pass to destination_table'
You can use multiple characters strings, which is what would kill your method:
my_str = "Take the source data from magicword and pass to magicword;
list1 = my_str.split('magicword')
params = ['temp_table', 'destination_table']
zipped = zip(list1, params)
replaced = ''.join([elt for sublist in zipped for elt in sublist])
In [25]: replaced
Out[25]: 'Take the source data from temp_table and pass to destination_table'
Note that if your params are shorter than the number of occurrences of your search string this will cut it
IN []my_str = "Take the source data from ? and pass to ?; then do again with ? and to ?"
Out[22]: 'Take the source data from temp_table and pass to destination_table'
Also note that the last bit after finding the string is deleted :(, something like
replaced = ''.join([elt for sublist in zipped for elt in sublist] + [list1[-1]])
Will do the trick
While #E. Serra's answer is a great answer, the comment from #jasonharper made me realize there's a MUCH simpler answer. So incredibly simple that I'm surprised that I completely missed it!
Instead of looping through the string, I should loop through the parameters. That'll allow me to replace the first instance of "?" with the current parameter that I'm looking at. Then I overwrite the string, allowing it to execute correctly on the next iteration.
Unlike the other solution posted, it won't cut off the end of my string either.
my_str = "Take the source data from ? and pass to ?;"
params = ['temp_table', 'destination_table']
for item in params:
query_str = query_str.replace('?', item, 1)

How to execute it correctly?

list1 = ['192,3.2', '123,54.2']
yx = ([float(i) for i in list1])
print(list1)
This is the code I have and I am trying to learn for future reference on how to remove , within a list of string. I tried various things like mapping but the mapping would not work due to the comma within the num.
If you want to remove commas from a string use :
list1 = string.split(",")
the string variable contains your string input, you get your output in the form a list, join the list if you want the original string without the commas.
string_joined = "".join(list1)
string_joined will contain your string without the commas.
If you want your string to just remove the comma and retain the empty space at that position, your syntax :
string = string.replace(","," ")
Also, the fist two syntax I explained, can be shortened to a single syntax :
string = string.replace(",","")
Now if you want to iterate in your list of strings, consider each element(string) in your list one at a time :
for string in list1 :
<your codes go here>
Hope this answers what you are looking for.
we can do regex to remove the non-digits to get rid of other characters
import regex as re
print([float(re.sub("[^0-9|.]", "", s)) for s in list1])
without regex:
[float(s.replace(',','')) for s in list1 ]
output:
[1923.2, 12354.2]

How to replace a list of words with a string and keep the formatting in python?

I have a list containing the lines of a file.
list1[0]="this is the first line"
list2[1]="this is the second line"
I also have a string.
example="TTTTTTTaaaaaaaaaabcccddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeefffff"
I want to replace list[0] with the string (example). However I want to keep the word length. For example the new list1[0] should be "TTTT TT TTa aaaaa aaaa". The only solution I could come up with was to turn the string example into a list and use a for loop to read letter by letter from the string list into the original list.
for line in open(input, 'r'):
list1[i] = listString[i]
i=i+1
However this does not work from what I understand because Python strings are immutable? What's a good way for a beginner to approach this problem?
I'd probably do something like:
orig = "this is the first line"
repl = "TTTTTTTaaaaaaaaaabcccddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeefffff"
def replace(orig, repl):
r = iter(repl)
result = ''.join([' ' if ch.isspace() else next(r) for ch in orig])
return result
If repl could be shorter than orig, consider r = itertools.cycle(repl)
This works by creating an iterator out of the replacement string, then iterating over the original string, keeping the spaces, but using the next character from the replacement string instead of any non-space characters.
The other approach you could take would be to note the indexes of the spaces in one pass through orig, then insert them at those indexes in a pass of repl and return a slice of the result
def replace(orig, repl):
spaces = [idx for idx,ch in enumerate(orig) if ch.isspace()]
repl = list(repl)
for idx in spaces:
repl.insert(idx, " ")
# add a space before that index
return ''.join(repl[:len(orig)])
However I couldn't imagine the second approach to be any faster, is certain to be less memory-efficient, and I don't find it easier to read (in fact I find it HARDER to read!) It also don't have a simple workaround if repl is shorter than orig (I guess you could do repl *= 2 but that's uglier than sin and still doesn't guarantee it'll work)

How to delete an empty list in a list? (Python)

I wrote a function which takes a .txt file.
The first thing it does is split the file at ',' and add them to a list, which creates a list of lists.I used:
lst = s.split(',')
I do get a list of lists, except every second line has an empty list ['\n']. I need to find a way get rid of these empty lists as they muck up the rest of the code.
Is there any simple way of doing this? Or is it just that I doing something wrong?
Any help would be greatly appreciated.
Thank You!
Sample Data:
1,2,3,4
,3,4,
Expected Output:
['1','2','3','4\n']
['','3','4','\n']
Current Output:
['1','2','3','4\n']
['\n']
['','3','4','\n']
Output after using sshashank124's suggestion:
['1','2','3','4\n']
[]
['','3','4','\n']
Output after using Alex Thornton's suggestion:
['1','2','3','4\n']
[]
['','3','4','\n']
Use strip() (or rstrip()) to get rid of new-line characters:
lst = s.strip().split(',')
See also: How can I remove (chomp) a newline in Python?
You can simply do it as:
lst = [i for i in s.split(',') if i != '\n']
Example
>>> s = 'hello,\n,world,\n,bye,\n,world'
>>> lst = [i for i in s.split(',') if i != '\n']
>>> print lst
['hello', 'world', 'bye', 'world']
lst = filter(lambda a: len(a)==1 and a[0]!='\n' or len(a)>1, lst)
This will clear the list of empty list [ ] or list containing only \n.
But it's probably better to clear the text of unwanted strings BEFORE splitting it to a list.
Edit
note at the end of code you have to replace the lst with your list containing the unwanted characters.
also you could try removing the \n from the original text file with this code.
text_file = text_file.replace('\n', '')
If your sample data doesn't have a blank line between the two populated lines, then it seems to me you are having a line-end issue upon reading the text file in the first place. If you post how you're reading in the text file, you could get answers that fix your problem before it ever gets to the point where you have any empty lists to remove.
That said, if lst really does just have [\n] as every other element, you can simply skip them as follows:
lst = s.strip().split(',')[::2]
(Note I've already incorporated the strip mentioned in previous answers to remove the newline characters.)

Add characters (',') every time a certain character ( , )is encountered ? Python 2.7.3

Let's say you had a string
test = 'wow, hello, how, are, you, doing'
and you wanted
full_list = ['wow','hello','how','are','you','doing']
i know you would start out with an empty list:
empty_list = []
and would create a for loop to append the items into a list
i'm just confused on how to go about this,
I was trying something along the lines of:
for i in test:
if i == ',':
then I get stuck . . .
In Python, the nicest way to do what you want is
full_list = test.split(', ')
If your string might have some commas that aren't followed by spaces, you would need to do something a little more robust. Maybe
full_list = [x.lstrip() for x in test.split(',')]
>>> test = 'wow, hello, how, are, you, doing'
>>> full_list = test.replace(",","','")
>>> print full_list
wow',' hello',' how',' are',' you',' doing
i just added the flanking quotations manually

Categories