Python read txt file and save it as numpy array or list

1, the read data, stored as an array numpy:

my_data = numpy.loadtxt('my_file.txt')

After numpy array is stored, the size can be my_data my_data.shapeview, with numpy.reshape()modified dimensions.

2, the read data, stored as a list of list:

with open('my_file.txt', 'r') as f:
    my_data = f.readlines() #txt中所有字符串读入data,得到的是一个list
    # 对list中的数据做分隔和类型转换
    for line in my_data:
       line_data = line.split()        
       numbers_float = map(float, line_data) #转化为浮点数

Guess you like

Origin www.cnblogs.com/liuxin0430/p/11922089.html