[英]Remove text:u from strings in python
我正在使用xlrd库将值从excel文件导入到python列表中。 我在excel文件中有一列,并且按行提取数据。 但是问题是我在列表中得到的数据是
list = ["text:u'__string__'","text:u'__string__'",.....so on]
我如何从中删除此text:u以获得带有字符串的自然列表?
这里使用python2.7编写代码
book = open_workbook("blabla.xlsx")
sheet = book.sheet_by_index(0)
documents = []
for row in range(1, 50): #start from 1, to leave out row 0
documents.append(sheet.cell(row, 0)) #extract from first col
data = [str(r) for r in documents]
print data
遍历项目并从每个单词中删除多余的字符:
s=[]
for x in list:
s.append(x[7:-1]) # Slice from index 7 till lastindex - 1
如果这是您的标准输入列表,则可以通过简单split
[s.split("'")[1] for s in list]
# if your string itself has got "'" in between, using regex is always safe
import re
[re.findall(r"u'(.*)'", s)[0] for s in list]
#Output
#['__string__', '__string__']
我有同样的问题。 以下代码对我有所帮助。
list = ["text:u'__string__'","text:u'__string__'",.....so on]
for index, item in enumerate(list):
list[index] = list[index][7:] #Deletes first 7 xharacters
list[index] = list[index][:-1] #Deletes last character
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.