Skip to content
Advertisement

python regex: capture different strings line by line from .txt file

I need to extract names/strings from a .txt file line by line. I am trying to use regex to do this.

Eg. In this below line I want to extract the name “Victor Lau”, “Siti Zuan” and the string “TELEGRAPHIC TRANSFER” in three different lists then output them into an excel file. You may see the txt file also

TELEGRAPHIC TRANSFER 0008563668 040122 BRH BDVI0093 VICTOR LAU 10,126.75- .00 10,126.75- SITI ZUZAN 16:15:09

I have tried this code

JavaScript

Advertisement

Answer

Your text file looks to be fixed width columns – no delimiters. You can use re capture groups like ‘^(.{20})(.{15})(.{30})’

or you can specify the columns start position and width and use that to splice out the data from each row.

This method will parse 2 columns from each line of your file and return an array of rows, each with an array of columns.

JavaScript

Output:

JavaScript

From here you can save the data any way you like.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement