0

I have a file .txt like this:

8.3713312149,0.806817531586,0.979428482338,0.20179159543
5.00263547897,2.33208847046,0.55745770379,0.830205341157
0.0087910592556,4.98708152771,0.56425779093,0.825598658777

and I want data to be saved in a 2d array such as

array = [[8.3713312149,0.806817531586,0.979428482338,0.20179159543],[5.00263547897,2.33208847046,0.55745770379,0.830205341157],[0.0087910592556,4.98708152771,0.56425779093,0.825598658777]

I tried with this code

#!/usr/bin/env python

checkpoints_from_file[][]

def read_checkpoints():
    global checkpoints_from_file
    with open("checkpoints.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            checkpoints_from_file.append(line.split(","))
        print checkpoints_from_file


if __name__ == '__main__':
    read_checkpoints()

but it does not work. Can you guys tell me how to fix this? thank you

2 Answers 2

3

You have two errors in your code. The first is that checkpoints_from_file[][] is not a valid way to initialize a multidimensional array in Python. Instead, you should write

checkpoints_from_file = [] 

This initializes a one-dimensional array, and you then append arrays to it in your loop, which creates a 2D array with your data.

You are also storing the entries in your array as strings, but you likely want them as floats. You can use the float function as well as a list comprehension to accomplish this.

checkpoints_from_file.append([float(x) for x in line.split(",")])
Sign up to request clarification or add additional context in comments.

Comments

1

Reading from your file,

def read_checkpoints():
    checkpoints_from_file = []
    with open("checkpoints.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            checkpoints_from_file.append(line.split(","))
        print(checkpoints_from_file)


if __name__ == '__main__':
    read_checkpoints()


Or assuming you can read this data successfully, using a string literal,

lines = """8.3713312149,0.806817531586,0.979428482338,0.20179159543
5.00263547897,2.33208847046,0.55745770379,0.830205341157
0.0087910592556,4.98708152771,0.56425779093,0.825598658777"""

and a list comprehension,

list_ = [[decimal for decimal in line.split(",")] for line in lines.split("\n")]

Expanded,

checkpoints_from_file = []
for line in lines.split("\n"):
    list_of_decimals = []
    for decimal in line.split(","):
        list_of_decimals.append(decimal)
    checkpoints_from_file.append(list_of_decimals)

print(checkpoints_from_file)

Your errors:

  1. Unlike in some languages, in Python you don't initialize a list like, checkpoints_from_file[][], instead, you can initialize a one-dimensional list checkpoint_from_file = []. Then, you can insert more lists inside of it with Python's list.append().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.