My list looks like this:
[',100,"","Rock outcrop","Miscellaneous area"']
I want to add in double quotes before the 100.
so that it looks like this:
['"",100,"","Rock outcrop","Miscellaneous area"']
I tried the insert function, but that only adds something to my list before the start of my list. When I did insert, it looked like this
['', ',100,"","Rock outcrop","Miscellaneous area"']
-
4It looks like you're starting with a list, but that list contains a single string that contains commas and quotes. Is that really what you want? Or do you want a list with four items, an integer and three strings?Greg Hewgill– Greg Hewgill2012年03月18日 00:58:55 +00:00Commented Mar 18, 2012 at 0:58
3 Answers 3
What you've got is a single string, containing
,100,"","Rock outcrop","Miscellaneous area"
inside a list. If what you want is to add "" to the beginning of that string, then you can do that by doing
mylist[0] = '""' + mylist[0]
But I assume that you probably want an actual sequence of strings, in which case you want
import ast
mylist = ast.literal_eval('""' + mylist[0])
#mylist is now ('', 100, '', 'Rock outcrop', 'Miscellaneous area')
ast.literal_eval interprets a string as a Python literal, in this case a tuple.
Comments
What you actually have is a list of one string. So just pull out the string.
s = l[0]
s = '""' + s
But... that's an odd use of a list and string. You might want to use a different structure.
Comments
Suppose you have the list
lst = [',100,"","Rock outcrop","Miscellaneous area"']
So what you can do is get the first element in the list using lst[0] and then changing it and assigning it back to lst[0], so
lst[0] = '""' + lst[0]
would do it
Edit: the problem you seem to be having is that you are making an array of one element that is a string. So you have a list with the element
',100,"","Rock outcrop","Miscellaneous area"'
which is a string what you probably want instead is to do something like
lst = [100,"","Rock outcrop","Miscellaneous area"]
and then do the insert