我正在编写一个小的Python脚本从数据库中收集一些数据,唯一的问题是当我从mysql导出数据为XML文件时,它会在XML文件中包含一个\b字符。我编写了代码将其删除,但后来意识到我不需要每次都这样处理,因此我将其放入一个方法中,并在遇到\b时调用它,但现在正则表达式无法匹配,尽管我知道有\b存在。
以下是我的做法:
主程序:
任何帮助都将不胜感激, 谢谢。
以下是我的做法:
主程序:
'''Program should start here'''
#test the file to see if processing is needed before parsing
for line in xml_file:
p = re.compile("\b")
if(p.match(line)):
print p.match(line)
processing = True
break #only one match needed
if(processing):
print "preprocess"
preprocess(xml_file)
预处理方法:
def preprocess(file):
#exporting from MySQL query browser adds a weird
#character to the result set, remove it
#so the XML parser can read the data
print "in preprocess"
lines = []
for line in xml_file:
lines.append(re.sub("\b", "", line))
#go to the beginning of the file
xml_file.seek(0);
#overwrite with correct data
for line in lines:
xml_file.write(line);
xml_file.truncate()
任何帮助都将不胜感激, 谢谢。
if(x):
,惯用的写法只是if x:
。 - unwind\b
(两个字符)吗? - Qtax