Skip to content

Commit 896de23

Browse files
committed
use an iterator to slice strings
1 parent 8765192 commit 896de23

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

pandas/io/json/_json.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from io import StringIO
44
from itertools import islice
55
import os
6-
from typing import Any, Callable, Iterator, Optional, Type
6+
import re
7+
from typing import Any, Callable, Optional, Type
78

89
import numpy as np
910

@@ -733,15 +734,6 @@ def _combine_lines(self, lines) -> str:
733734
lines = filter(None, map(lambda x: x.strip(), lines))
734735
return "[" + ",".join(lines) + "]"
735736

736-
def _jsonstring_to_list_generaor(self, data: str) -> Iterator[str]:
737-
prev_index = -1
738-
while True:
739-
next_index = data.find("\n", prev_index + 1)
740-
if next_index < 0:
741-
break
742-
yield data[prev_index + 1 : next_index]
743-
prev_index = next_index
744-
745737
def read(self):
746738
"""
747739
Read the whole JSON input into a pandas object.
@@ -751,7 +743,9 @@ def read(self):
751743
elif self.lines:
752744
data = ensure_str(self.data)
753745
if self.nrows:
754-
data = list(islice(self._jsonstring_to_list_generaor(data), self.nrows))
746+
compiled_pattern = re.compile("\n")
747+
data_iterator = compiled_pattern.finditer("data")
748+
data = list(islice(data_iterator, self.nrows))
755749
else:
756750
data = data.split("\n")
757751
obj = self._get_object_parser(self._combine_lines(data))

0 commit comments

Comments
 (0)