Reputation: 2719
I have an input string as:
result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
data = result.split("\n")
i = 0
while i < len(data):
i = i +1
dd = data[i].split(',')
print dd
break
And the corresponding output as:
[
'"testing"',
'"0.8841"',
'"642000.0"',
'"80.014521"',
'"-60.940653"',
'"4522126666"',
'"1500854400"',
'""',
'"1500842014000"',
'"name"',
'"80.014521"',
'"-60.996532"',
'"sampledevice"',
'"3"',
'"name"'
]
How can I remove the single quotes from each element in the list?
Upvotes: 6
Views: 50349
Reputation: 140297
you need to apply strip
to remove quotes on both sides of the string.
dd = [x.strip('"') for x in data[i].split(',')]
that said, your loop seems to have an index issue. Should be rewritten, for instance like this:
result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400"\n"1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
for line in result.splitlines():
dd = [x.strip('"') for x in line.split(',')]
print(dd)
at this point, you'd be even better off with
dd = ast.literal_eval(line)
also perfect usage of csv
module with a list
as input (no need to pass a file handle) (don't pass a string
, though, as it yields some strange effects)
import csv
for row in csv.reader(result.splitlines()):
print(row)
all resulting in:
['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400']
['1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name']
Upvotes: 6
Reputation: 46779
Treat the text as a row from a CSV formatted file:
import csv
import StringIO
result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
print next(csv.reader(StringIO.StringIO(result)))
Giving you:
['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400', '', '1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name']
Python's StringIO()
function allows the text to be treated like a file allowing it to be passed to Python's CSV parser which is designed for parsing CSV files in this format. It can then correctly parse the text and return a list of items.
The returned data could then be further processed if needed to convert the text into numbers, i.e. integers or floats as appropriate. For example:
import csv
import StringIO
def convert(text):
try:
return int(text)
except ValueError:
pass
try:
return float(text)
except ValueError:
return text
result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
values = [convert(value) for value in next(csv.reader(StringIO.StringIO(result)))]
print values
This would then return a list as follows:
['testing', 0.8841, 642000.0, 80.014521, -60.940653, 4522126666L, 1500854400, '', 1500842014000L, 'name', 80.014521, -60.996532, 'sampledevice', 3, 'name']
Upvotes: 7
Reputation: 4652
While statement is weird. Doesn't he want to delete single quotes? Why does everyone post answers which delete double quotes.
Here is my suggestion:
result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
data = result.split("\n")
s = str(data)[2:-2] #Convert to a string, and delete [' and ']
for dd in s.split(','):
print dd
Or in a simpler way, this would make the same thing:
for dd in result.split(','):
print(dd)
The result:
"testing"
"0.8841"
"642000.0"
"80.014521"
"-60.940653"
"4522126666"
"1500854400"
""
"1500842014000"
"name"
"80.014521"
"-60.996532"
"sampledevice"
"3"
"name"
Upvotes: 2
Reputation: 2888
First: You don't have single quotes in output string. What do You have is string which is quoted with double quotes (possibly for storing in csv
file). Single quotes You see in output are actually determining the string. So:
'"some_string"'
is actually
"some_string"
Second: if You want to remove double quotes, You can do:
for item in data.split(","):
print(item.strip('"'))
Look at the following statements, it should be more clear:
item = '"some_string"'
other_item = 'some_string'
some_other_item = "some_string"
print(item) # --> "some_string"
print(other_item) # --> some_string
print(some_other_item) # --> some_string
print(item, other_item, some_other_item) # --> ('"some_string"', 'some_string', 'some_string')
Upvotes: 2
Reputation: 1941
literal_eval is good solution for this issue
import ast
dd = [ast.literal_eval(i) for i in data]
Upvotes: 6
Reputation: 78554
Replace the double quotes before splitting:
>>> result.replace('"', '').split(',')
['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400', '', '1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name']
Upvotes: 5