how to remove semicolon at the end of element from text file to list

2.4k Views Asked by At

How to remove this semicolon(;) from a text file. content of my text file:

    201911007,1,28;
    201203008,1,28;
    199710014,1,28;
    201612010,1,28;
    201710017,1,28;

Then python reads it it always have semicolon at the end of 28 if I run the codes

    with open("empMR.txt", 'r') as files:
    for dln in files:
        dln = dln.strip()
        if len(dln) >= 1:
            lii = dln.split(",")
            MR_empno.append(lii[0].strip())
            month.append(lii[1].strip())
            days_work.append(lii[2].strip())
print(days_work)

OUTPUT: ['28;', '28;', '28;', '28;', '28;'] I want to remove the semicolon(;) on each output Expected output[28, 28, 28, 28, 28]

4

There are 4 best solutions below

0
On

If you want to remove the ; permanently from your file. Open the file with vim

vi filename
then press esc
enter :%s/;$//g then enter
save the file esc-->:wq --> enter

If you want to handle it in python

with open('filename.txt','r') as fp:
    data=fp.read()
data = data.splitlines()
data = [line.strip(';') for line in data]
0
On

Regular expressions are powerful tools for strings.

import re
in_list = ['28;', '28;', '28;', '28;', '28;']
out_list = [re.sub(';', '', item) for item in in_list]
print(out_list)
['28', '28', '28', '28', '28']
0
On

There are a handful of ways you could tackle this.

  • Take a look at the .replace() method, something like dln.replace(';', '') could be helpful
  • If all of your lines look the same, you could remove the last character - dln = dln[:-1] could be helpful

Which one you choose depends on how confident you are that the data will always look the same, and which methods you're more comfortable with

0
On

just include the characters you want to strip (including ;)

for dln in files:
    dln = dln.strip("; \n\r\t")

you dont need to add extra calls to other functions, and especially dont need regular expressions just for this (there might be other valid reasons to use regular expressions, see example below)

data = re.findall("([^,]*)\s*,\s*([^,]*)\s*,\s*([^;]*)\s*;",files.read())
for empNo,monthNo,days_work in data:
    print("E:",empNo, "Month:",monthNo, "Days Worked:",days_work)