I'm trying to get a final pandas data frame from an initial uniprot url:
import requests
url = 'http://www.uniprot.org/uniprot/?query=Interferon+lambda+receptor+1&sort=score&format=tab'
req = requests.get(url)
The output of:
req.text
is something like this:
""Entry\tEntry name\tStatus\tProtein names\tGene names\tOrganism\tLength\nQ8IU57\tINLR1_HUMAN\treviewed\tInterferon lambda receptor 1 (IFN-lambda receptor 1) (IFN-lambda-R1) (Cytokine receptor class-II member 12) (Cytokine receptor family 2 member 12) (CRF2-12) (Interleukin-28 receptor subunit alpha) (IL-28 receptor subunit alpha) (IL-28R-alpha) (IL-28RA) (Likely interleukin or cytokine receptor 2).....
To get the lines I did:
lines = req.text.splitlines()
#every line is separated by a comma ' ', ' ', ... #columns are separated by '\t'
If I use:
import re
re.split(r'\t+', lines[0])
this gives to the correct splitting of each columns.
Out:
['Entry',
'Entry name',
'Status',
'Protein names',
'Gene names',
'Organism',
'Length']
However, if I want to do a for loop and do it on all lines I get an error: string2list(lines): list indices must be integers or slices, not str'
import re
def string2list(file):
list = []
for i in lines:
re.split(r'\t+', lines[i])
list += lines
return list
My aim is to get a list of lists to finally use this code:
import pandas as pd
list_name = lines
df = pd.DataFrame (list_name, columns = lines[i])
Any ideas on what is the best approach? Is the conversion of string in a list to a list of list possible? what is the best way? Or is there another way to reach the pandas data frame directly from the url? Thank you in advance!