Home > Net >  To see all integers in same format inside list using python
To see all integers in same format inside list using python

Time:09-07

Data inside text file(input.txt) is in below format data :

11,12,14, 15,17,18

In python script using this above data as input. Below is my code

doReg = ""
with open("input.txt", "r") as er:
    for line in er:
        if line.strip(): 
            key, value = [x.strip() for x in line.strip().split(':', 6)]
            if key == "data":
                doReg = value

doReg = doReg.strip(",")
duo = doReg.split(",")

Output of duo is

['910', '911', '913', ' 903', '904', '905']

I am looking to data as

['910', '911', '913', '903', '904', '905']

How to remove space inside quotes?

CodePudding user response:

duo =['910', '911', '913', ' 903', '904', '905']
duo = [s.strip(' ') for s in duo]
print(duo)

output:

['910', '911', '913', '903', '904', '905']

CodePudding user response:

doReg = ""
with open("input.txt", "r") as er:
    for line in er:
        if line.strip(): 
            key, value = [x.strip() for x in line.strip().split(':', 6)]
            if key == "data":
                doReg = value
tem = doReg.split(" ")
doReg = tem[0] tem[1]
duo = doReg.split(",")

CodePudding user response:

Just add .strip(' ') before splitting each line to remove the extra space:

doReg = ""
with open("input.txt", "r") as er:
    for line in er:
        if line.strip(): 
            key, value = [x.strip() for x in line.strip().strip(' ').split(':', 6)]
            if key == "data":
                doReg = value

doReg = doReg.strip(",")
duo = doReg.split(",")
  • Related