I'm writing a python function to append data to text file, as shown in the following,
The problem is the variable, var, could be a 1D numpy array, a 1D list, or just a float number, I know how to convert numpy.array/list/float to string separately (meaning given the type), but is there a method to convert var to string without knowing its type?
def append_txt(filename, var):
my_str = _____ # convert var to string
with open(filename,'a') as f:
f.write(my_str + '\n')
Edit 1: Thanks for the comments, sorry maybe my question was not clear enough.
str(var) on numpy would give something like []. For example, var = np.ones((1,3)), str(var) will give [[1. 1. 1.]], and [] is unwanted,
Edit 2: Since I want to write clean numbers (meaning no [ or ]), it seems type checking is inevitable.
str(var)?