简体   繁体   English

Python-将字符串转换为列表

[英]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? 我怎样才能做到这一点?

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') : 您可以使用encode('ascii','ignore')删除unicodes,但请注意,首先需要为python澄清您的字符串是unicode,然后可以使用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']

Try putting this on the top of your code: 尝试将其放在代码的顶部:

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

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. 假设g1是包含字符串的数组,而g是包含整个文件作为字符串的变量。

I got my answer from: 我的回答来自:

Python: Sanitize a string for unicode? Python:为Unicode字符串消毒?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM