Py学习  »  TomNash  »  全部回复
回复总数  2

使用正则表达式提取不同的组并根据需要重新排序。

import re
new_lines = []
with open('inputdata.txt', 'r') as file:
    for line in file:
        regex = re.compile(r'(\w+),\s?(Mr.|Miss.|Mrs.)\s(\w+\s?\w*\s),([01])')
        new_line = regex.findall(line)[0]
        print(f"{new_line[0]},{new_line[2]},{new_line[3]},{new_line[1]}")

结果:

Braund,Owen Harris ,1,Mr.
Heikkinen,Laina ,0,Miss.
Allen,William Henry ,0,Mr.

这在纽比到 base_repr() :

import numpy as np
[np.base_repr(100, base) for base in range(2,11)]

结果:

['1100100', '10201', '1210', '400', '244', '202', '144', '121', '100']