10 examples of 'python read json file line by line' in Python

Every line of 'python read json file line by line' 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
191def load_jsonlines(file_name, file_path):
192 """Load a jsonlines file, yielding data line by line.
193
194 Parameters
195 ----------
196 file_name : str
197 File from which to load data.
198 file_path : str
199 Path to directory from which to load.
200
201 Yields
202 ------
203 dict
204 Dictionary of data loaded from file.
205 """
206
207 with open(fpath(file_path, fname(file_name, 'json')), 'r') as f_obj:
208
209 while True:
210
211 # Load each line, as JSON file
212 try:
213 yield load_json(f_obj, '')
214
215 # Break off when get a JSON error - end of the file
216 except JSONDecodeError:
217 break
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

116def json_loads(line):
117 return json.loads(line.decode("utf-8","ignore"))
34def load_lines(input_file):
35 with open(input_file, 'r') as read:
36 lines = read.read()
37 return lines
224def _read_json(self, input_file):
225 with open(input_file, "r", encoding="utf-8") as fin:
226 lines = fin.readlines()
227 return lines
77def _find_line(string, file_name):
78 """
79 find the first occurence of a string in a file
80 """
81
82 with open(file_name, 'r') as fi:
83 for line in fi:
84 if string in line:
85 return line
43def read_data(log_file):
44 """
45 Read, parse and return the JSON data for the given log file name.
46 (As a side effect sets the global _last_log_file to the log file name.)
47 """
48 global _last_log_file
49 try:
50 with open(log_file, "r") as f:
51 f.readline()
52 data = json.load(f)
53 # Keep track of the current log file in global variable in case we need to
54 # identify it later if there's a problem. (This works because the files are
55 # processed lazily.)
56 _last_log_file = log_file
57 except IOError:
58 sys.exit("Could not read log file %s" % log_file)
59 return data
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
24def load_json(self,json_file_path):
25 fin = open(json_file_path,"r")
26 try:
27 json_obj = json.load(fin)
28 self.log4py.info("加载了%s文件"%json_file_path)
29 except ValueError, e:
30 json_obj = {}
31 fin.close()
32 return json_obj
48def read_line(self, line):
49 """Read in a single line and return a (directive, arguments) tuple"""
50 directive, arguments = line.split(':')
51 arguments = [a.strip() for a in arguments.split('->')]
52 self.read_directive(directive.strip(), *arguments)
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()]

Related snippets