Added done Work
This commit is contained in:
44
read_file.py
Normal file
44
read_file.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
|
||||
def read_xy_file(path):
|
||||
"""
|
||||
Reads a 2-column text file and returns x and y arrays.
|
||||
Assumes the first line is a header.
|
||||
"""
|
||||
x_vals = []
|
||||
y_vals = []
|
||||
|
||||
with open(path, "r") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# Skip header (line 0)
|
||||
for line in lines[1:]:
|
||||
if not line.strip():
|
||||
continue # skip empty lines
|
||||
parts = line.split()
|
||||
if len(parts) < 2:
|
||||
continue # skip malformed lines
|
||||
|
||||
x_vals.append(float(parts[0]))
|
||||
y_vals.append(float(parts[1]))
|
||||
|
||||
return x_vals, y_vals
|
||||
|
||||
def read_data_with_indices(filename):
|
||||
"""
|
||||
Reads a file with one float per line and returns two arrays:
|
||||
- data_array: the floats from the file
|
||||
- index_array: the corresponding indices
|
||||
"""
|
||||
# Read the data into a numpy array
|
||||
data_array = np.loadtxt(filename, dtype=float)
|
||||
|
||||
# Generate an array of indices
|
||||
index_array = np.arange(len(data_array))
|
||||
|
||||
return data_array, index_array
|
||||
|
||||
if __name__ == "__main__":
|
||||
x, y = read_xy_file("prak2/test.txt")
|
||||
print(x)
|
||||
print(y)
|
||||
Reference in New Issue
Block a user