0

If I have a text file containing the following numbers:

5.078780 5.078993
7.633073 7.633180
2.919274 2.919369
3.410284 3.410314

How can read it and store it in an array, so that it becomes:

[[5.078780,5.078993],[7.633073,7.633180],[2.919274,2.919369],[3.410284,3.410314]]
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • What have you tried so far? Any idea about relevant functions to use etc.? Try breaking the problem down into two parts: reading the data from a file and formatting the data. – peachykeen Mar 20 '19 at 20:58

8 Answers8

1
with open('test.txt', 'r') as file:
    output = [ line.strip().split(' ') for line in file.readlines()]

# Cast strings to floats
output = [[float(j) for j in i] for i in output]    
print(output)

should give the desired output:

[[5.07878, 5.078993], [7.633073, 7.63318], [2.919274, 2.919369], [3.410284, 3.410314]]
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

This should work,

with open('filepath') as f:
    array = [line.split() for line in f.readlines()]
Alakazam
  • 461
  • 5
  • 14
geckos
  • 5,687
  • 1
  • 41
  • 53
0

Approach: Have a result list = [] Split the text by newlines \n. Now in a for-loop split each line by a space char and assign to a tuple append tuple to the result list I'm refraining from writing code here to let you work it out.

perennial_noob
  • 459
  • 3
  • 14
0

This should do

with open ("data.txt", "r") as myfile:
    data=myfile.readlines()

for i in range(len(data)):
    data[i]=data[i].split()
Juan C
  • 5,846
  • 2
  • 17
  • 51
0

Python provides the perfect module for this, it's called csv:

import csv

def csv_to_array(file_name, **kwargs):
    with open(file_name) as csvfile:
        reader = csv.reader(csvfile, **kwargs)
        return [list(map(float, row)) for row in reader]


print(csv_to_array('test.csv'))

If you later have a file with a different field separator, say ";", then you'll just have to change the call to:

print(csv_to_array('test.csv', delimiter=';'))

Note that if you don't care about importing numpy then this solution is even better.

cglacet
  • 8,873
  • 4
  • 45
  • 60
0

You first want to retrieve the file content in an array of string (each string is one line of the file)

with open("myfile.txt", 'r') as f: file_content = f.readlines()

Refer to open doc for more: https://docs.python.org/3/library/functions.html#open

Then you want to create a list

content_list = []

And then you want to fill it with each string, when each string should be split with a space(using split() function) which make a list with the two values and add it to content_list, use a for loop !

for line in file_content:
    values = line.split(' ') # split the line at the space
    content_list.append(values)

By the way, this can be simplified with a List Comprehension:

content_list = [s.split(' ') for s in file_content]
Alakazam
  • 461
  • 5
  • 14
0

To convert to this exact format :

with open('filepath', 'r') as f:
    raw = f.read()

arr = [[float(j) for j in i.split(' ')] for i in raw.splitlines()]
print arr

outputs :

[[5.07878, 5.078993], [7.633073, 7.63318], [2.919274, 2.919369], [3.410284, 3.410314]]
-1
with open('blah.txt', 'r') as file:
    a=[[l.split(' ')[0], l.split(' ')[1] for l in file.readlines() ]
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50