I developed a python code that generates several arrays of data. I want to save these arrays in a specific location on my computer and then import them into another notebook. It's important that, given a matrix saved as "data.csv", I can import caluns or rows from it. For example data[0]
or data[0][1]
for example.
I've already tried to do it in a way where I save the data as follows:
with open(path '/data_Sevol.csv', 'w', newline='') as csvfile:
fieldnames = ['Sevol']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for i in range(len(Sevol)):
writer.writerow({'Sevol': Sevol[i]})
However, although I can define a variable that receives the matrix, when calling a row or column it goes wrong.
Does anyone know of a more convenient way to save data so that when importing it I can simply call a row, column or element in the usual way (eg data[i][j]
).
Thanks for any help.
CodePudding user response:
If you are trying to store dictionary, you can consider using json, this can be done by import json
You can dump dictionary into a string, and load them from file very quickly.
You can learn about how to use json here:
https://www.w3schools.com/python/python_json.asp
If you are using csv, the result of open() csv cannot be directly used like a list, you instead need a function to turn it into a nested list so you can use indexing
import csv
def import_csv(path):
with open(path,"r") as raw_data:
result = []
data = csv.reader(raw_data,delimiter=",")
for row in data:
result.appendd(row)
return result
This function takes the path of a csv file (I assume , is the delimiter) and return a nested list for you to use data[i][j]
.
However take note that the header is also included, so remember to exclude the first row before you iterate the data
I read your comment and wrote these set of function, calling export_csv()
will save an array with the datatype, if you use import_csv()
it should return a nested list while preserving the datatype. It only work for int, str, float and bool for now:
import csv
def get_type(inp):
t = str(type(inp))
return t.split("'")[1][0]
def fix_type(inp):
t = inp[0]
if t == "i":
return int(inp[1:])
elif t == "s":
return str(inp[1:])
elif t == "f":
return float(inp[1:])
elif t == "b":
return bool(inp[1:])
def transform(array):
result = []
for row in array:
tem = []
for item in row:
tem.append(get_type(item) str(item))
result.append(tem)
return result
def deform(array):
result = []
for row in array:
tem = []
for item in row:
tem.append(fix_type(item))
result.append(tem)
return result
def import_csv(path):
with open(path,"r") as raw_data:
result = []
data = csv.reader(raw_data,delimiter=",")
for row in data:
result.append(row)
return deform(result)
def export_csv(path,data):
with open(path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for i in transform(data):
writer.writerow(i)