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

Every line of 'python read file line by line 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
42def readLines(in_file):
43 """Returns a list of lines from a input markdown file."""
44
45 with open(in_file, 'r') as inf:
46 in_contents = inf.read().split('\n')
47 return in_contents
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

27def read_lines(filepath):
28 """
29 :param filepath: (string) path to a file, each line will be an item.
30 :return: (list).
31 """
32 ls = []
33 with open(filepath) as f:
34 for line in f:
35 line = line.strip()
36 ls.append(line)
37 return ls
130def getLineList(self):
131 if self.__bufferSize:
132 raise NotImplementedError
133 return self.__lineList
60def _read_file_lines(file_):
61 """Read lines of file
62 file_: either path to file or a file (like) object.
63 Return list of lines read or 'None' if file does not exist.
64 """
65 cont_str = _read_file(file_)
66 if cont_str is None:
67 return None
68 return [url_str.rstrip() for url_str in cont_str.splitlines()]
170def _read_line1(line):
171 """Reads the first line of a thermdat specie
172
173 Parameters
174 ----------
175 line : str
176 Line 1 of thermdat specie
177 Returns
178 -------
179 nasa_data : dict
180 Nasa input fields
181 """
182 nasa_data = {}
183 ref_pos = 24
184 ref_offset = 5
185 max_elements = 4
186 phase_pos = 44
187
188 # Store the name
189 blank_pos = line.find(' ')
190 nasa_data['name'] = line[:blank_pos]
191
192 # Store the notes if any
193 notes = line[blank_pos:ref_pos].strip()
194 if len(notes) > 0:
195 nasa_data['notes'] = notes
196
197 # Store the elements
198 nasa_data['elements'] = {}
199 for i in range(max_elements):
200 blank_pos = line.find(' ', ref_pos)
201 # All the elements have been assigned
202 if blank_pos == ref_pos:
203 break
204
205 element = line[ref_pos:blank_pos]
206 ref_pos += ref_offset
207 coeff = int(line[blank_pos:ref_pos])
208
209 nasa_data['elements'][element] = coeff
210
211 # Store the phase
212 nasa_data['phase'] = line[phase_pos]
213
214 # Store the temperatures
215 fields = _get_fields(line[phase_pos + 1:])
216 nasa_data['T_low'] = float(fields[0])
217 nasa_data['T_high'] = float(fields[1])
218 nasa_data['T_mid'] = float(fields[2])
219 return nasa_data
11def read_input(file):
12 for line in file:
13 yield line.rstrip()
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
42def read_line(filename):
43 """help function to read a single line from a file. returns none"""
44 try:
45 f = open(filename)
46 line = f.readline().strip()
47 f.close()
48 return line
49 except IOError:
50 return None
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
34def load_lines(input_file):
35 with open(input_file, 'r') as read:
36 lines = read.read()
37 return lines

Related snippets