1. 程式人生 > >匯入txt檔案

匯入txt檔案

def import_txt():
    fileName = QFileDialog.getOpenFileName(self, "", "C:\\Users\\Administrator\\Desktop")
    if fileName[0]:
        ret = []
        with open(fileName[0], "r", encoding="utf-8") as f:
            for line in f.readlines():
                if line.strip():
                    line 
= line.rstrip() pal = re.compile(u'[\u4e00-\u9fa5]+') match = pal.search(line) if not match: hm = re.sub(r'(\s+?)(\w)', r',\2', line) hm = hm.split(',') for i in hm:
if re.match(r"^([\d|X][\d|X][\d|X][\d|X])", i[0:4]): zsnum = re.match(r'\d+', i[5:]) ziduan1 = i[0:4] hm_nomey = int(zsnum.group()) ret.append("%s=%s" % (ziduan1, hm_nomey))
return ret