Home > other >  getting rid of apostrophe around numbers while converting a text file into a list
getting rid of apostrophe around numbers while converting a text file into a list

Time:07-14

I have a text file like this:

textfile:

841                                                                                                                                                                                                                     
842
843
844
845
846

I am using the code below to convert it into a list:

with open("textfile") as file:
    lines = file.readlines()
    lines = [line.rstrip() for line in lines]

But the problem is: "lines" is not exactly a list of numbers, its something like: ['841', '842', '843', '844', '845', '846']

However, I need something like: [841, 842, 843, 843, 844, 845, 846]

Can anyone help me out with how can i get rid of '' around each number?

CodePudding user response:

Your text file is read in as text, so the variables you are getting are stored as text, strings. The "" around your variables denotes this.

In order to get them to display (and likely operate) in the way you want, you'll need to convert them to an integer. That's normally done like this:

number = "185"
number = int(number)

In your case, this can be incorporated like so:

with open("textfile") as file:
    lines = file.readlines()
    lines = [int(line.rstrip()) for line in lines]

CodePudding user response:

Like this:

with open("textfile.txt") as file:
    lines = file.readlines()
    lines = [int(line.rstrip()) for line in lines]
print(lines)
print(type(lines[0]))

Output

[841, 842, 843, 844, 845, 846]
<class 'int'>

CodePudding user response:

You can use int function to convert it into integer like this:

with open("textfile") as file:
    lines = file.readlines()
    lines = [int(line.rstrip()) for line in lines]

CodePudding user response:

So since files contain strings which are represented with either single or double quotes, your list is being populated with strings.

We can convert these into integers by casting each string in the list to an int and add it to a new list like so:

int_list = []    # create a new list
        for word in lines:    # loop through each word in the lines list
            if word.isdigit():   # check that the characters in the string all all digits
                int_list.append(int(word))   # if so, append to new list

This should fix your issue and now you have a list of integers in int_list

So your final code should be:

with open("textfile") as file:
    lines = file.readlines()
    lines = [line.rstrip() for line in lines]

    int_list = []
        for word in lines:
            if word.isdigit():
                int_list.append(int(word))

CodePudding user response:

First, there is no need to use readlines to read the file if you're going to iterate over it anyway. Iterating over a text file gives it to you one line at a time. So you could simplify your initial code to:

with open("textfile") as file:
    lines = [line.rstrip() for line in file]

Now the only thing left is to convert each line of text into an int:

with open("textfile") as file:
    lines = [int(line.rstrip()) for line in file]

CodePudding user response:

use the int function to convert string to integer

  • Related