Skip to main content
 首页 » 编程设计

python之xml解析和 latex 符号

2025年05月04日20freeliver54

我正在读取一些 xml 并且信息存储为

H.P. Dembinski, B. K\'{e}gl, I.C. Mari\c{s}, M. Roth, D. Veberi\v{c} 

我想以那种格式得到它,但是我得到了

u"H.P. Dembinski, B. K\\'{e}gl, I.C. Mari\\c{s}, M. Roth, D. Veberi\\v{c}" 

所以我的问题是我的处理步骤哪里出错了? 这是我做的

today = some date 
base_url = "http://export.arxiv.org/oai2?verb=ListRecords&" 
url = (base_url + "from=%s&until=%s&" % (today, today) + "metadataPrefix=arXivRaw") 
 
try: 
    response = urllib2.urlopen(url) 
 
except urllib2.HTTPError, e: 
    return 
 
rawdata = response.read() 
root = ET.fromstring(rawdata) 
 
if root.find(OAI+'ListRecords') is not None: 
   for record in root.find(OAI+'ListRecords').findall(OAI+"record"): 
     author_string = info.find(ARXIVRAW+"authors").text 

如何防止引入第二个\?我知道我可以做一些替换(),但必须有一种方法来获取原始文本吗? 谢谢 卡尔

请您参考如下方法:

您只是看到了字符串的内部表示,它使用双 \\ 来显示转义的反斜杠。如果打印该字符串,您应该只会看到一个 \。示例 -

>>> print(u"H.P. Dembinski, B. K\\'{e}gl, I.C. Mari\\c{s}, M. Roth, D. Veberi\\v{c}") 
H.P. Dembinski, B. K\'{e}gl, I.C. Mari\c{s}, M. Roth, D. Veberi\v{c} 

您还可以注意到开头的 u 表示它是 unicode 字符串的内部表示。

所以没问题,即使在写入文件等时也是如此。它应该工作正常,应该只有一个 \ 出现。