我需要逐行从.txt文件中提取名称/字符串。我想用regex来做这件事。
例如:在下面的这一行,我想提取“维克多刘”,“西提赞”和字符串“电汇”在三个不同的列表,然后输出到一个excel文件。你可能会看到txt文件也
电汇0008563668 040122 BDVI0093刘荣华10,126.75- .00 10,126.75- SITI ZUZAN 16:15:09
我试过这个代码
for file in os.listdir(directory):
filename = os.fsdecode(file)
if (filename.endswith(".txt") or filename.endswith(".TXT")) and (filename.find('AllBanks')!=-1):
with open(file) as AllBanks:
for line in AllBanks:
try:
match4 = re.search(r'( [a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+ )|( [a-zA-Z]+ [a-zA-Z]+)', line)
List4.append(match4.group(0).strip())
except:
List4.append('NA')
df = pd.DataFrame(np.column_stack([List4,List5,List6]),columns=['a', 'b', 'c'])
df.to_excel('AllBanks.xlsx', index=False)发布于 2022-03-11 05:08:23
您的文本文件看起来是固定宽度列-没有分隔符。您可以使用像‘^(.{20})(.{15})(.{30})这样的re捕获组。
或者您可以指定列的起始位置和宽度,并使用它们将每一行的数据拼接出来。
此方法将从文件的每一行解析2列,并返回一个行数组,每个行都包含一个列数组。
def parse(filename):
fixed_columns = [[0, 28], [71, 50]] # start pos and width pairs of columns you want
rows = []
with open(filename) as file:
for line in file:
cols = []
for start,wid in fixed_columns:
cols.append(line[start: start+wid].strip())
rows.append(cols)
return rows
for row in parse(filename):
print(", ".join(row))输出:
TELEGRAPHIC TRANSFER, LIEW WAI KEEN
TELEGRAPHIC TRANSFER, KWAN SANG@KWAN CHEE SANG
TELEGRAPHIC TRANSFER, VICTOR LAU
TELEGRAPHIC TRANSFER, VICTOR LAU从这里开始,您可以以任何方式保存数据。
https://stackoverflow.com/questions/71433019
复制相似问题