read data from file to a 2d array [Python] - python

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

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(",")])

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:
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().

Related

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate.
The file is structured as follows:
'''
#Comment line 1
#Comment line 2
1.00000000,3.02502604,343260.68655952,384.26845401,-7.70828175,-0.45288215
2.00000000,3.01495320,342124.21684440,767.95286901,-7.71506536,-0.45123853
3.00000000,3.00489957,340989.57100678,1151.05303883,-7.72185550,-0.44959182
'''
I would like to obtain the numbers from the last two columns and convert this into separate arrays or lists, I tried doing this by creating an empty list, but I do not know how to make this from a log file with a certain name. Could someone help me with this as I am a beginner programmer?
The expected output I would like to obtain is:
list1 = [-7.70828175, -7.71506536, -7.71506536]
list2 = [-0.45288215, -0.45123853, -0.44959182]
Thank you in advance!
Try this way. but you have to confirm that each row list length must equal to 6.
list1 = []
list2 = []
with open('example.log') as f:
for i in f.readlines():
if (len(i.split(',')) == 6):
list1.append(i.split(',')[4])
list2.append(i.split(',')[5])
print(list1)
print(list2)

AttributeError: 'tuple' object has no attribute 'write' Error

I keep getting this error and I have no idea what it means. I have taken measures to get rid of a tuple in my code. The program is supposed to read in a document which has a series of numbers and then sort those numbers using a bubble sort function and then print the old list and the new, sorted list onto a new file.
My assignment is to create a new file and print the original array, from the given file, and the sorted array, sorted using a bubble sort function, as two lines in a comma separated file.
# reading in the document into the program
file = open("rand_numb.csv", "r")
# creating a new file that will have the output printed on it
newFile = ("SORTED.csv", "w+")
# creating a blank list that will hold the original file's contents
orgArr = []
# creating the bubblesort function
def bubblesort(arr):
# creating a variable to represent the length of the array
length = len(arr)
# traverse through all array elements
for i in range(length):
# last i elements are already in place
for j in range(0, length-i-1):
# traverse the array from 0 to length-i-1 and swap if the element found is greater than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
# Prog4 Processing
# using a for loop to put all of the numbers from the read-in file into a list
listoflists = [(line.strip()).split() for line in file]
# closing the original file
file.close()
# creating a variable to represent the length of the list
listLen = len(listoflists)
# using a for loop to have the elements in the list of lists into one list
for num in range(0, listLen):
orgArr.append(num)
# using list function to change the tuple to a list
orgArr = list(orgArr)
# using the bubblesort function
sortArr = bubblesort(orgArr)
# Prog4 Output
# outputting the two lists onto the file
newFile.write(orgArr + "\n")
newFile.write(sortArr)
# closing the new file
newFile.close() ```
Rather than create a new file in your line:
newFile = ("Sorted.csv", "w+")
you have instead defined a tuple containing two strings "Sorted.csv" and "w+" by declaring these comma separated values between parenthesis. Rather than create your newFile at the top of your code, you can wait to create it until you actually intend to populate it.
with open("Sorted.csv", "w+") as newFile:
newFile.write(orgArr + "/n")
newFile.write(sortArr)
newFile.close()
I suspect you may have issues that your newFile is formatting how you want, but I will let you raise a new question in the event that that is true.

.split from a file and putting it in an array

Im reading a file with some information and each part is separated with a # however on each line i want it to be a different array so i did this and im not sure why its not working.
main_file = open("main_file.txt","r")
main_file_info=main_file.readlines()
test=[]
n=0
for line in main_file_info:
test[n]=line.split("#")
test=test[n][1:len(test)-1] # to get rid of empty strings at the start and the end
print(test)# see what comes out
main_file.close()
The way you are inserting the output of line.split("#") in your list is wrong. Your list is not initialized, hence, you can't simply assign anything to any element of the list. So, what you need to do is this :
test.append(line.split("#"))
Or, you can initialize your list as below :
test = [[]]*(len(main_file_info))
test = [None for _ in range(total)]
# instead of test = []
or simply just append to test:
test.append( line.split("#") )

Python Dynamic Data Structures

I am going to read the lines of a given text file and select several chunks of data whose format are (int, int\n) . Every time the number of lines are different so I need a dynamic sized data structure in Python. I also would like to store those chunks in 2D data structures. If you are familiar with MATLAB programming, I'd like to have something like a structure A{n} n = number of chunks of data and each chunk includes several lines of the data mentioned above.
Which type of data structure would you recommend? and how to implement with it?
i.e. A{0} = ([1,2],[2,3],[3,4]) A{1} = ([1,1],[2,2],[5,5],[7,4]) and so on.
Thank you
A python list can contain lists as well any different data type.
l = []
l.append(2) # l is now (2)
l.extend([3,2]) # l is now (2,3,2)
l.append([4,5]) # l is now (2,3,2,[4,5])
list.Append appends whatever it is given as argument to the list
while list.extend makes the given the argument the tail of the list.
I guess you required list would appear somehwhat like this:
l = ([[1,2],[2,3],[3,4]],[[1,1],[2,2],[5,5],[7,4]])
PS: Here's a link to get you jump start learning python
https://learnxinyminutes.com/docs/python/
Just keep in mind that if your are reading data from text file , the format is string , you need to use int() to convert your string to int.
The issue was resolved with 2 steps appending the list.
import numpy as np
file = ('data.txt')
f = open(file)
i = 0
str2 = '.PEN_DOWN\n'
str3 = '.PEN_UP\n'
A = []
B = []
for line in f.readlines():
switch_end = False
if (line == str2) or (~switch_end):
if line[0].isdigit():
A.append(line[:-1])
elif line == str3:
switch_end = True
B.append(A)
A = []
B.append(A)
f.close()
print(np.shape(A))
print(np.shape(B))

Import a txt file into a 2d list of floats

I ve got a txt file of float numbers and I want to opened it in a python script into a 2d list of floats.How is it possible to store a txt to 2d list of float? I am using the following code:
arrays = []
for line in open('normRowsRaw.txt'):
# use a list comprehension to build your array on the fly
new_array = np.array((array.float(i) for i in line.split(' ')))
arrays.append(new_array)
and my file is like the above series:
0.0098506, 0.068954, 0.007388, 0.0049253, 0.0049253, 0.054178, 0.014776, 0.014776,
0.014776, 0.0024627, 0.007388, 0.0098506, 0.007388, 0.022164, 0.029552, 0.017239,
0.0024627, 0.019701, 0.0098506, 0.0024627, 0, 0.068954, 0.0098506, 0.11082, 0.0024627...
You could try this
arrays = []
with open('normRowsRaw.txt') as f:
f = f.read().split(',')
for n in f:
arrays.append(float(n.strip()))
Edit
this appends to a normal list not 2d
You can use a list comprehension to convert each string to float and return the 2D array as a numpy array.
import numpy as np
with open('normRowsRaw.txt') as file:
array2d = np.array([[float(digit) for digit in line.split(",")] for line in file])
This is the way is would do it:
for line in open('normRowsRaw.txt'):
# use a list comprehension to build your array on the fly
array = line.split(',')
del array[len(array)-1] #to remove \n from array
new_array = []
for i in range(0,len(array)-1):
new_array.append(float(array[i]))
#After all elements are added to an array, add array to result
arrays.append(new_array)

Categories

Resources