从URL解析表而没有(编码?)奇怪的字符

我正在尝试从网址页面解析一个表,该页面可以在here中找到。

下面是我用来解析表格的(注释)代码:

## Imports (some of them might not be used in this code snippet
import urllib.request,json 
import pandas as pd
import bleach
import html.parser
import time

# Read the whole url
with urllib.request.urlopen("https://www.mitomap.org/foswiki/bin/view/MITOMAP/MutationsCodingControl") as url:
    data = url.read().decode()

## Specify start and end of the string that contains the table data
start = '"data":[' 
end = '}]}' 

## slice the data into a string specified by start and end
final_string = '{' + data[data.index(start):data.index(end) + len(end)]

## remove the html tags and special characters
clean = html.unescape(bleach.clean(final_string,tags=[],strip=True))

## convert the clean str into a dictionary
json_dict = json.loads(clean)

## convert now json_dict into a df
df = pd.DataFrame(data=json_dict['data'],columns=[json_dict['columns'][i]['title'].strip() for 
i in list(range(len(json_dict['columns'])))])

上面的代码部分起作用:我确实得到了具有所需结构的pandas df,尽管我在列名方面遇到了问题。如果我这样做:json_dict['columns']我得到:

[{'title': ' Position ','type': 'num-html'},{'title': ' Locus ','type': 'string','class': 'select-filter'},{'title': ' Disease ','type': 'string'},{'title': ' Allele  ',{'title': ' NucleotideChange ',{'title': ' Amino\xa0AcidChange ',{'title': ' Homoplasmy ',{'title': ' Heteroplasmy ',{'title': ' Status ',{'title': ' GB\xa0Freq\xa0\xa0FL\xa0(CR)*‡ ',{'title': ' GB\xa0Seqs\xa0FL\xa0(CR)* ',{'title': ' References ','type': 'num-html'}]

如果我这样做df.columns,我会得到:

Index(['Position','Locus','Disease','Allele','NucleotideChange','Amino AcidChange','Homoplasmy','Heteroplasmy','Status','GB Freq  FL (CR)*‡','GB Seqs FL (CR)*','References'],dtype='object')

这意味着“怪异”字符仍然存在(例如GB Freq FL (CR)*‡

最后,如果我做df['Amino AcidChange'](因为Amino AcidChangedf.columns中的一个元素),我得到KeyError: 'Amino AcidChange'

我可能用json_dict替换regex中的'奇怪'字符,尽管我想知道:

  1. 这些“怪异”字符从哪里来?它与编码有关吗?
  2. 如何解析data中的信息(可能通过更改传递给url.read().decode()的参数)而没有那些'奇怪'字符?
klf0818 回答:从URL解析表而没有(编码?)奇怪的字符

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160719.html

大家都在问