天天看點

python讀取txt檔案中的數字_python将txt檔案中的字元和數字單獨提取

import re

def getDigiStr(file_path):

fp = open(file_path, 'r')

file_text = fp.read()

digi_str = re.findall(r'([0-9]+)',file_text,re.MULTILINE)

fp.close()

#數字

return ''.join(digi_str)

def getLetterStr(file_path):

fp = open(file_path, 'r')

file_text = fp.read()

letter_str = re.findall(r'([a-zA-Z]+)',file_text,re.MULTILINE)

fp.close()

#字母

return ''.join(letter_str)

if __name__ == '__main__':

print(getDigiStr(r'test.txt'))

print(getLetterStr(r'test.txt'))