简体   繁体   English

Python从xlrd创建的列表中删除“文本”

[英]Python remove 'text' from lists made by xlrd

I used xlrd to read through each cell of three columns to make three lists. 我使用xlrd读取了三列的每个单元格,从而形成了三个列表。 Then, I appended the ith element of all three lists to a new list, making a new list of lists. 然后,我将所有三个列表的第i个元素附加到新列表中,从而创建了一个新的列表列表。

search_terms=[]
for row in range(0, book.nrows):
    search_terms.append([med_name[row], med_school[row], mentor[row]])
print(*search_terms[0:15], sep='\n')
[text:'Andrew Burkeland', 'Weill Cornell Medical College', 'Dave Cutler ']
[text:'Andrew Pence', 'University of Alabama at Birmingham School of Medicine', 'Jack Warran ']

Is there a way to take out the 'text:'? 有没有办法删除“文本:”? I am inputing each list in search_terms into Entrez.egquery to search for results on pubmed , and with 'text:' in the query line, I keep on getting 0 results. 我将search_terms每个列表输入到Entrez.egquery以搜索pubmed结果,并且在查询行中使用“ text:”,我一直得到0个结果。

Let's assume a simple table which is called 'students.xlsx' 让我们假设一个简单的表叫做“ students.xlsx”

Student     School     Mentor
John Doe    Harvard    Kornberg
Jane Done   Stanford   Pauling

and now open it with xlrd 现在用xlrd打开它

import xlrd
xl_workbook = xlrd.open_workbook('students.xlsx')
xl_sheet = xl_workbook.sheet_by_index(0)
row = xl_sheet.row(1)

Now let's look at the individual parts 现在让我们看一下各个部分

print(row)

[text:'John Doe', text:'Harvard', text:'Kornberg']

print(row[0])

text:'John Doe'

print(row[0].value)

'John Doe'

The problem is that row[0] is an xlrd cell and not a string which is reason why it is necessary to get the content via value . 问题是row[0]xlrd单元格,而不是字符串,这就是为什么有必要通过value获得内容的原因。

Now let's do it for all rows (except the header): 现在让我们对所有行(标头除外)执行此操作:

raw_data = list()
for row in range(1, xl_sheet.nrows):
    raw_data.append(xl_sheet.row(row))

author_list = list()
for raw in raw_data:
    author_list.append(list())
    for r in raw:
        author_list[-1].append(r.value)
print(author_list)
 > [['John Doe', 'Harvard', 'Kornberg'], ['Jane Done', 'Stanford', > 'Pauling']] 

or short: 或简称:

author_list = [[c.value for c in xl_sheet.row(n)] for n in range(1, xl_sheet.nrows)]

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

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