Convert plain numbers from file to list of integers

Christos Karapapas

I have a text file with some plain numbers that i want them as integers in a list.
For example i'd like this
299 314 427
to be converted to this
list = [[299], [314], [427]]
i read the text file
with open('C:/...', 'r') as f:
masses= f.read()
and then i use a for loop and spectra.split(' ') since a blank space is the only thing that separates the integers but it appeds to list every number separately

list = []   
masses.split(' ')  
for mass in masses:  
    list.append(mass)

the results is ['2','9','9',' ','3',......] how can i split them and append them in list ?

falsetru

Using list comprehension:

>>> with open('FILEPATH') as f:
...     lst = [[int(n)] for n in f.read().split()]
... 
>>> lst
[[299], [314], [427]]

Without argument, str.split splits strings by consecutive whitespace (space, tab, newline, ..):

>>> 'a\t\tb\nc  d'.split()
['a', 'b', 'c', 'd']
>>> 'a\t\tb\nc  d'.split(' ')
['a\t\tb\nc', '', 'd']

Don't use list as a variable name. It shadows the builtin function list.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

convert list of strings from file to list of integers

From Dev

convert list of strings from file to list of integers

From Dev

Convert a string into a list with integers including negative numbers

From Dev

Convert all numbers to positives in a List of integers

From Dev

convert string of numbers into list of integers in python

From Java

Haskell List Comprehension, delete integers from List of numbers

From Dev

Convert a list of integers to string

From Dev

Python Numpy can't convert Strings to Integers from CSV file

From Dev

Convert RTF to and from plain text

From Dev

Turning numbers in a file into integers with Python

From Dev

Reading certain integers from a text file into a Array List

From Dev

How to convert string to a list of integers?

From Dev

Convert a list of integers to a bytearray in Python

From Dev

Convert list of strings into tuple of integers

From Dev

Given a list of integers and an integer k, return whether k can be built as a sum of any numbers from that list or their multiplications

From Dev

Parse data from multiple numbers in a list or slice - TypeError: list indices must be integers or slices, not str

From Dev

Remove integers from list

From Dev

Import file and convert integers into int

From Dev

convert saved string of numbers into an array of integers

From Dev

Adding integers from a file

From Dev

Convert list of numbers to characters

From Dev

Convert list of numbers to characters

From Dev

Convert list of numbers to code

From Dev

How to convert file data to plain hex?

From Dev

convert list with arrays from csv file into floats

From Dev

Convert JSON from file to List<T>

From Dev

Convert list of integers to list of bits Python

From Dev

python convert string representation of nested list of integers to nested list of integers

From Dev

From List of Set of String to a plain list

Related Related

  1. 1

    convert list of strings from file to list of integers

  2. 2

    convert list of strings from file to list of integers

  3. 3

    Convert a string into a list with integers including negative numbers

  4. 4

    Convert all numbers to positives in a List of integers

  5. 5

    convert string of numbers into list of integers in python

  6. 6

    Haskell List Comprehension, delete integers from List of numbers

  7. 7

    Convert a list of integers to string

  8. 8

    Python Numpy can't convert Strings to Integers from CSV file

  9. 9

    Convert RTF to and from plain text

  10. 10

    Turning numbers in a file into integers with Python

  11. 11

    Reading certain integers from a text file into a Array List

  12. 12

    How to convert string to a list of integers?

  13. 13

    Convert a list of integers to a bytearray in Python

  14. 14

    Convert list of strings into tuple of integers

  15. 15

    Given a list of integers and an integer k, return whether k can be built as a sum of any numbers from that list or their multiplications

  16. 16

    Parse data from multiple numbers in a list or slice - TypeError: list indices must be integers or slices, not str

  17. 17

    Remove integers from list

  18. 18

    Import file and convert integers into int

  19. 19

    convert saved string of numbers into an array of integers

  20. 20

    Adding integers from a file

  21. 21

    Convert list of numbers to characters

  22. 22

    Convert list of numbers to characters

  23. 23

    Convert list of numbers to code

  24. 24

    How to convert file data to plain hex?

  25. 25

    convert list with arrays from csv file into floats

  26. 26

    Convert JSON from file to List<T>

  27. 27

    Convert list of integers to list of bits Python

  28. 28

    python convert string representation of nested list of integers to nested list of integers

  29. 29

    From List of Set of String to a plain list

HotTag

Archive