10 examples of 'python read file into array' in Python

Every line of 'python read file into array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
9def read_file():
10 # Text file containing words for training
11 training_file = 'belling_the_cat.txt'
12 content=[]
13 with open(training_file,'r') as f:
14 for line in f.readlines():
15 # line 表示读到数据的每一行,linelist是按照空格切分成一个list
16 linelist=line.strip().split()
17 for i in linelist:
18 content.append(i.strip())
19 content=np.array(content)
20 content=np.reshape(content,[-1,]) #shape (204,1)
21 return content
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

53def read_file(f):
54 """
55 Open the text file containing deep learning classification results for files. The decision variables is automatically opened from utils folder
56 :param f: text filename
57 :returns:
58 - sounds: sounds that were analyzed
59 - lc: labels for each sound
60 """
61 with open(f, 'r') as i_file:
62 files = i_file.read().split('\n')
63 sounds = np.array([i.split(" ")[0].split(".json")[0] for i in np.array(files)])
64 labels = np.array([i.split(" ") for i in np.array(files)])
65 lc = []
66 for lb in labels:
67 try:
68 lc.append(lb[1])
69 except:
70 continue
71 decisions = np.loadtxt('utils/data.txt')
72 return sounds, np.array(lc), decisions
50def file_reader(filename, shape, data_type = None, **kwds):
51 # TODO: write the reader...
52
53 dc = np.fromfile(filename, **kwds)
54 if len(shape) == 3:
55 dc = dc.reshape((shape[2], shape[1], shape[0])).swapaxes(1,2)
56 if data_type is None:
57 data_type = 'SI'
58 elif len(shape) == 2:
59 dc = dc.reshape(shape).T
60 if data_type is None:
61 data_type = 'Image'
62 return [{'mapped_parameters':{
63 'data_type' : data_type,
64 'name' : filename,
65 },
66 'data':dc,
52def readArray(file, count, func):
53 result = []
54
55 for i in range(count):
56 result.append(func(file))
57
58 return result
4def read_array_from_binary_file(filename, shape, dtype=numpy.float64, order='F'):
5 array = numpy.fromfile(filename, dtype=dtype)
6 if array.shape[0] != shape[0]*shape[1]*shape[2]:
7 raise ValueError("Incorrect grid size %d x %d x %d" %shape)
8 array = array.reshape(shape, order=order)
9 return array
164def file_read(fname, arr, size):
165 return
13def read_file(filename):
14 return np.matrix([map(float, line.strip('\n').split('\t')) for line in open(filename)])
1049def _array_from_file(infile, dtype, count, sep):
1050 """Create a numpy array from a file or a file-like object."""
1051
1052 if isfile(infile):
1053
1054 global CHUNKED_FROMFILE
1055 if CHUNKED_FROMFILE is None:
1056 if sys.platform == 'darwin' and LooseVersion(platform.mac_ver()[0]) < LooseVersion('10.9'):
1057 CHUNKED_FROMFILE = True
1058 else:
1059 CHUNKED_FROMFILE = False
1060
1061 if CHUNKED_FROMFILE:
1062 chunk_size = int(1024 ** 3 / dtype.itemsize) # 1Gb to be safe
1063 if count < chunk_size:
1064 return np.fromfile(infile, dtype=dtype, count=count, sep=sep)
1065 else:
1066 array = np.empty(count, dtype=dtype)
1067 for beg in range(0, count, chunk_size):
1068 end = min(count, beg + chunk_size)
1069 array[beg:end] = np.fromfile(infile, dtype=dtype, count=end - beg, sep=sep)
1070 return array
1071 else:
1072 return np.fromfile(infile, dtype=dtype, count=count, sep=sep)
1073 else:
1074 # treat as file-like object with "read" method; this includes gzip file
1075 # objects, because numpy.fromfile just reads the compressed bytes from
1076 # their underlying file object, instead of the decompressed bytes
1077 read_size = np.dtype(dtype).itemsize * count
1078 s = infile.read(read_size)
1079 return np.fromstring(s, dtype=dtype, count=count, sep=sep)
83def read_file_and_fill_arr(file_descr, arr):
84 for line in file_descr.readlines():
85 if line.startswith('#'): continue
86 phi_str, psi_str, value_str = line.split()
87 phi = int(float(phi_str))
88 psi = int(float(psi_str))
89 value = float(value_str)
90 arr[(phi+179)//2][(psi+179)//2] = value
5def read_h5_array(f5, var_name):
6
7 try:
8 nx = f5['Nx'].value
9 ny = f5['Ny'].value
10 nz = f5['Nz'].value
11 except:
12 nx,ny,nz = np.shape(f5[var_name])
13 return f5[var_name]
14
15 # column-ordered data with image convention (x horizontal, y vertical, ..)
16 # i.e., so-called fortran ordering
17 val = f5[var_name][:]
18
19 #print("reshaping 1D array of {} into multiD with {} {} {}".format(len(val), nx,ny,nz))
20
21 # reshape to python format; from Fortran image to C matrix
22 val = np.reshape(val, (nz, ny, nx))
23 val = val.ravel(order='F').reshape((nx,ny,nz))
24
25 return val

Related snippets