|
3 | 3 | from io import StringIO
|
4 | 4 | from itertools import islice
|
5 | 5 | import os
|
6 |
| -import re |
7 | 6 | from typing import Any, Callable, Optional, Type
|
8 | 7 |
|
9 | 8 | import numpy as np
|
@@ -507,6 +506,7 @@ def read_json(
|
507 | 506 | The number of lines from the line-delimited jsonfile that has to be read.
|
508 | 507 | This can only be passed if `lines=True`.
|
509 | 508 | If this is None, all the rows will be returned.
|
| 509 | +
|
510 | 510 | .. versionadded:: 1.1
|
511 | 511 |
|
512 | 512 | Returns
|
@@ -688,9 +688,9 @@ def _preprocess_data(self, data):
|
688 | 688 | If self.chunksize, we prepare the data for the `__next__` method.
|
689 | 689 | Otherwise, we read it into memory for the `read` method.
|
690 | 690 | """
|
691 |
| - if hasattr(data, "read") and not self.chunksize: |
| 691 | + if hasattr(data, "read") and (not self.chunksize or not self.nrows): |
692 | 692 | data = data.read()
|
693 |
| - if not hasattr(data, "read") and self.chunksize: |
| 693 | + if not hasattr(data, "read") and (self.chunksize or self.nrows): |
694 | 694 | data = StringIO(data)
|
695 | 695 |
|
696 | 696 | return data
|
@@ -738,17 +738,17 @@ def read(self):
|
738 | 738 | """
|
739 | 739 | Read the whole JSON input into a pandas object.
|
740 | 740 | """
|
741 |
| - if self.lines and self.chunksize: |
742 |
| - obj = concat(self) |
743 |
| - elif self.lines: |
744 |
| - data = ensure_str(self.data) |
745 |
| - if self.nrows: |
746 |
| - compiled_pattern = re.compile(".*\n") |
747 |
| - data = (line.group(0) for line in compiled_pattern.finditer(data)) |
748 |
| - data = islice(data, self.nrows) |
| 741 | + if self.lines: |
| 742 | + if self.chunksize: |
| 743 | + obj = concat(self) |
| 744 | + elif self.nrows: |
| 745 | + lines = list(islice(self.data, self.nrows)) |
| 746 | + lines_json = self._combine_lines(lines) |
| 747 | + obj = self._get_object_parser(lines_json) |
749 | 748 | else:
|
| 749 | + data = ensure_str(self.data) |
750 | 750 | data = data.split("\n")
|
751 |
| - obj = self._get_object_parser(self._combine_lines(data)) |
| 751 | + obj = self._get_object_parser(self._combine_lines(data)) |
752 | 752 | else:
|
753 | 753 | obj = self._get_object_parser(self.data)
|
754 | 754 | self.close()
|
|
0 commit comments