-3

I have a file say input.txt which contains data in the following format ::

[8, 3, 4, 14, 19, 23, 10, 10, "Delhi"]
13
"Delhi"

8
10
19

How can i read the data in python or ruby. And I can see that my first row contains data which contains both integer and string. And also how can I store it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

1 Answers1

1

If you trust the source of your input text file, then I notice that each line is a valid Python expression so you could do something like:

with open(filename) as txt:
    evaluated_lines = [eval(line) for line in txt if line.strip()]
print(evaluated_lines)

The output is:

[[8, 3, 4, 14, 19, 23, 10, 10, 'Delhi'], 13, 'Delhi', 8, 10, 19]

Note that the Python list datatype can contain a mixture of sub-lists, integers, and strings

Paddy3118
  • 4,704
  • 27
  • 38