David Washington
David Washington

Reputation: 119

Python - Turning a string into a list

I'm trying to turn this string into a list:

f = open( "animals.txt", "r")
g = f.read()
g1 = g.split(",")
print g1 # list of words

I'm getting:

['\x93SHEEP\x94', '\x94TIGER\x94', '\x94LION\x94', '\x94DEER\x94',
'\x94PIG\x94', '\x94DOG\x94', '\x94CAT\x94', '\x94SHARK\x94',
'\x94RAT\x94', '\x94EEL\x94']

What I want is:

['SHEEP', 'TIGER', 'LION', 'DEER', 'PIG', 'DOG', 'CAT', 'SHARK', 'RAT', 'EEL']

How can I do this?

Upvotes: 0

Views: 124

Answers (3)

jkd
jkd

Reputation: 1045

Try escaping your strings with:

g.decode("unicode-escape")

or:

for i in range(0,len(g1)):
    g1[i] = g1[i].decode("unicode-escape")

This is assuming g1 is the array containing the strings and g is the variable containing the whole file as a string.

I got my answer from:

Python: Sanitize a string for unicode?

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107287

You can use encode('ascii','ignore') to remove unicodes , but note that first you need to clarify for python that your strings are unicode you can do it with decode('unicode_escape') :

>>> l
['\x93SHEEP\x94', '\x94TIGER\x94', '\x94LION\x94', '\x94DEER\x94', '\x94PIG\x94', '\x94DOG\x94', '\x94CAT\x94', '\x94SHARK\x94', '\x94RAT\x94', '\x94EEL\x94']
>>> [i.decode('unicode_escape').encode('ascii','ignore') for i in l]
['SHEEP', 'TIGER', 'LION', 'DEER', 'PIG', 'DOG', 'CAT', 'SHARK', 'RAT', 'EEL']

Upvotes: 3

taesu
taesu

Reputation: 4580

Try putting this on the top of your code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Upvotes: 0

Related Questions