0

I have data in text file :

2,58 1,23 0,14
6,58 4,2  1,3

I want to have this data from my text file in list written in this format:

[[2, 58, 1, 23, 0, 14]
 [6, 58, 4, 2, 1, 3]]

I tried this :

folder = open('text.txt', encoding = 'utf-8')
data = [numbers.strip().replace(',',' ').split(' ') for numbers in folder]
folder.close
print(data)

But I received result like this : [['2', '58', '1', '23', '0', '14']['6', '58', '4', '2', '1', '3']]

If I'm trying to set int() to numbers in many places in list I receiving this error : int() argument must be a string, a bytes-like object or a number, not list

So I need just change all string in this list from str to int, can you help me, please?

Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
abcd14
  • 13
  • 1

1 Answers1

0

Try to use map() function it takes two arguments where one is function, condition, etc and the second is an iterable objects like list, set, etc so in your question will be like map (int ,yourlist) and after cast it into list like list(map (int ,yourlist)) for more see this question to know why you should cast it

Example

folder = open('text.txt', encoding = 'utf-8')
data = [list(map(int, numbers.strip().replace(',',' ').split(' '))) for numbers in folder]
folder.close()
print(data)
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39