-
Notifications
You must be signed in to change notification settings - Fork 240
/
file_wrap.py
1086 lines (882 loc) · 33 KB
/
file_wrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
A collection of utilities for file wrapping.
Note: This is a work in progress.
"""
import re
from pyparsing import CaselessLiteral, Combine, OneOrMore, Optional, \
TokenConverter, Word, nums, oneOf, printables, ParserElement, alphanums
import numpy as np
def _getformat(val):
"""
Get the output format for a floating point number.
The general format is used with 16 places of accuracy, except for when
the floating point value is an integer, in which case a decimal point
followed by a single zero is used.
Parameters
----------
val : float or int
the number which needs formatted.
Returns
-------
string
the format string.
"""
if int(val) == val:
return "%.1f"
else:
return "%.16g"
class _SubHelper(object):
"""
Replaces file text at the correct word location in a line.
This class contains the Helper Function that is passed to re.sub.
Attributes
----------
_newtext : str
text to insert.
_replace_location : int
location in the file where replacement is to occur.
_current_location : int
current location in the file.
_counter : int
counter
_start_location : int
initial location where replacement is to occur.
_end_location : int
final location where replacement is to occur.
"""
def __init__(self):
"""
Initialize attributes.
"""
self._newtext = ""
self._replace_location = 0
self._current_location = 0
self._counter = 0
self._start_location = 0
self._end_location = 0
def set(self, newtext, location):
"""
Set a new word location and value for replacement.
Parameters
----------
newtext : str
text to insert.
location : int
location in the file where replacement is to occur.
"""
self._newtext = newtext
self._replace_location = location
self._current_location = 0
def set_array(self, newtext, start_location, end_location):
"""
Set a new starting location, ending location, and value for replacement.
Parameters
----------
newtext : str
text to insert.
start_location : int
location
end_location : int
location
"""
self._newtext = newtext
self._start_location = start_location
self._end_location = end_location
self._current_location = 0
def replace(self, text):
"""
Replace text in file.
This function should be passed to re.sub.
Parameters
----------
text : str
text to insert.
Returns
-------
string
newtext if current location is replace location else the input text.
"""
self._current_location += 1
if self._current_location == self._replace_location:
if isinstance(self._newtext, float):
return _getformat(self._newtext) % self._newtext
else:
return str(self._newtext)
else:
return text.group()
def replace_array(self, text):
"""
Replace array of text values in file.
This function should be passed to re.sub.
Parameters
----------
text : str
text to insert.
Returns
-------
string
newtext if current location is replace location else the input text.
"""
self._current_location += 1
end = len(self._newtext)
if self._current_location >= self._start_location and \
self._current_location <= self._end_location and \
self._counter < end:
if isinstance(self._newtext[self._counter], float):
val = self._newtext[self._counter]
newval = _getformat(val) % val
else:
newval = str(self._newtext[self._counter])
self._counter += 1
return newval
else:
return text.group()
class _ToInteger(TokenConverter):
"""
Converter for PyParsing that is used to turn a token into an int.
"""
def postParse(self, instring, loc, tokenlist):
"""
Convert token into an integer.
Parameters
----------
instring : str
the input string
loc : int
the location of the matching string
tokenlist : list
list of matched tokens
Returns
-------
int
integer value for token.
"""
return int(tokenlist[0])
class _ToFloat(TokenConverter):
"""
Converter for PyParsing that is used to turn a token into a float.
"""
def postParse(self, instring, loc, tokenlist):
"""
Convert token into a float.
Parameters
----------
instring : str
the input string
loc : int
the location of the matching string
tokenlist : list
list of matched tokens
Returns
-------
float
float value for token.
"""
return float(tokenlist[0].replace('D', 'E'))
class _ToNan(TokenConverter):
"""
Converter for PyParsing that is used to turn a token into Python nan.
"""
def postParse(self, instring, loc, tokenlist):
"""
Convert token into Python nan.
Parameters
----------
instring : str
the input string
loc : int
the location of the matching string
tokenlist : list
list of matched tokens
Returns
-------
float
the float value for NaN.
"""
return float('nan')
class _ToInf(TokenConverter):
"""
Converter for PyParsing that is used to turn a token into Python inf.
"""
def postParse(self, instring, loc, tokenlist):
"""
Convert token into Python inf.
Parameters
----------
instring : str
the input string
loc : int
the location of the matching string
tokenlist : list
list of matched tokens
Returns
-------
float
the float value for infinity.
"""
return float('inf')
class InputFileGenerator(object):
"""
Utility to generate an input file from a template.
Substitution of values is supported. Data is located with a simple API.
Attributes
----------
_template_filename : str or None
the name of the template file.
_output_filename : str or None
the name of the output file.
_delimiter : int
delimiter.
_reg : int
regular expression.
_data : list of string
the contents of the file, by line
_current_row : int
the current row of the file
_anchored : bool
indicator that position is relative to a landmark location.
"""
def __init__(self):
"""
Initialize attributes.
"""
self._template_filename = None
self._output_filename = None
self._delimiter = " "
self._reg = re.compile('[^ \n]+')
self._data = []
self._current_row = 0
self._anchored = False
def set_template_file(self, filename):
"""
Set the name of the template file to be used.
The template file is also read into memory when this method is called.
Parameters
----------
filename : str
Name of the template file to be used.
"""
self._template_filename = filename
templatefile = open(filename, 'r')
self._data = templatefile.readlines()
templatefile.close()
def set_generated_file(self, filename):
"""
Set the name of the file that will be generated.
Parameters
----------
filename : str
Name of the input file to be generated.
"""
self._output_filename = filename
def set_delimiters(self, delimiter):
"""
Set the delimiters that are used to identify field boundaries.
Parameters
----------
delimiter : str
A string containing characters to be used as delimiters.
"""
self._delimiter = delimiter
self._reg = re.compile('[^' + delimiter + '\n]+')
def mark_anchor(self, anchor, occurrence=1):
"""
Mark the location of a landmark.
This lets you describe data by relative position. Note that a forward
search begins at the old anchor location. If you want to restart the
search for the anchor at the file beginning, then call ``reset_anchor()``
before ``mark_anchor``.
Parameters
----------
anchor : str
The text you want to search for.
occurrence : int, optional
Find nth instance of text; default is 1 (first). Use -1 to
find last occurrence. Reverse searches always start at the end
of the file no matter the state of any previous anchor.
"""
if not isinstance(occurrence, int):
raise ValueError("The value for occurrence must be an integer")
instance = 0
if occurrence > 0:
count = 0
max_lines = len(self._data)
for index in range(self._current_row, max_lines):
line = self._data[index]
# If we are marking a new anchor from an existing anchor, and
# the anchor is mid-line, then we still search the line, but
# only after the anchor.
if count == 0 and self._anchored:
line = line.split(anchor)[-1]
if line.find(anchor) > -1:
instance += 1
if instance == occurrence:
self._current_row += count
self._anchored = True
return
count += 1
elif occurrence < 0:
max_lines = len(self._data) - 1
count = max_lines
for index in range(max_lines, -1, -1):
line = self._data[index]
# If we are marking a new anchor from an existing anchor, and
# the anchor is mid-line, then we still search the line, but
# only before the anchor.
if count == max_lines and self._anchored:
line = line.split(anchor)[0]
if line.find(anchor) > -1:
instance += -1
if instance == occurrence:
self._current_row = count
self._anchored = True
return
count -= 1
else:
raise ValueError("0 is not valid for an anchor occurrence.")
raise RuntimeError("Could not find pattern %s in template file %s" %
(anchor, self._template_filename))
def reset_anchor(self):
"""
Reset anchor to the beginning of the file.
"""
self._current_row = 0
self._anchored = False
def transfer_var(self, value, row, field):
"""
Change a single variable in the template relative to the current anchor.
Parameters
----------
value : float, int, bool, str
New value to set at the location.
row : int
Number of lines offset from anchor line (0 is anchor line).
This can be negative.
field : int
Which word in line to replace, as denoted by delimiter(s).
"""
j = self._current_row + row
line = self._data[j]
sub = _SubHelper()
sub.set(value, field)
newline = re.sub(self._reg, sub.replace, line)
self._data[j] = newline
def transfer_array(self, value, row_start, field_start, field_end,
row_end=None, sep=", "):
"""
Change the values of an array in the template relative to the current anchor.
This should generally be used for one-dimensional or free form arrays.
Parameters
----------
value : float, int, bool, str
Array of values to insert.
row_start : int
Starting row for inserting the array. This is relative
to the anchor, and can be negative.
field_start : int
Starting field in the given row_start as denoted by
delimiter(s).
field_end : int
The final field the array uses in row_end.
We need this to figure out if the template is too small or large.
row_end : int, optional
Use if the array wraps to cover additional lines.
sep : int, optional
Separator to use if we go beyond the template.
"""
# Simplified input for single-line arrays
if row_end is None:
row_end = row_start
sub = _SubHelper()
for row in range(row_start, row_end + 1):
j = self._current_row + row
line = self._data[j]
if row == row_end:
f_end = field_end
else:
f_end = 99999
sub.set_array(value, field_start, f_end)
field_start = 0
newline = re.sub(self._reg, sub.replace_array, line)
self._data[j] = newline
# Sometimes an array is too large for the example in the template
# This is resolved by adding more fields at the end
if sub._counter < len(value):
for val in value[sub._counter:]:
newline = newline.rstrip() + sep + str(val)
self._data[j] = newline
# Sometimes an array is too small for the template
# This is resolved by removing fields
elif sub._counter > len(value):
# TODO - Figure out how to handle this.
# Ideally, we'd remove the extra field placeholders
raise ValueError("Array is too small for the template.")
def transfer_2Darray(self, value, row_start, row_end, field_start, field_end):
"""
Change the values of a 2D array in the template relative to the current anchor.
This method is specialized for 2D arrays, where each row of the array is
on its own line.
Parameters
----------
value : ndarray
Array of values to insert.
row_start : int
Starting row for inserting the array. This is relative
to the anchor, and can be negative.
row_end : int
Final row for the array, relative to the anchor.
field_start : int
Starting field in the given row_start as denoted by
delimiter(s).
field_end : int
The final field the array uses in row_end.
We need this to figure out if the template is too small or large.
"""
sub = _SubHelper()
i = 0
for row in range(row_start, row_end + 1):
j = self._current_row + row
line = self._data[j]
sub.set_array(value[i, :], field_start, field_end)
newline = re.sub(self._reg, sub.replace_array, line)
self._data[j] = newline
sub._current_location = 0
sub._counter = 0
i += 1
# TODO - Note, we currently can't handle going beyond the end of
# the template line
def clearline(self, row):
"""
Replace the contents of a row with the newline character.
Parameters
----------
row : int
Row number to clear, relative to current anchor.
"""
self._data[self._current_row + row] = "\n"
def generate(self, return_data=False):
"""
Use the template file to generate the input file.
Parameters
----------
return_data : bool
If True, generated file data will be returned as a string.
Returns
-------
string
The generated file data if return_data is True or output filename
has not been provided, else None.
"""
if self._output_filename:
with open(self._output_filename, 'w') as f:
f.writelines(self._data)
else:
return_data = True
if return_data:
return '\n'.join(self._data)
else:
return None
class FileParser(object):
"""
Utility to locate and read data from a file.
Parameters
----------
end_of_line_comment_char : str, optional
End-of-line comment character to be ignored
(e.g., Python supports in-line comments with "#").
full_line_comment_char : str, optional
Comment character that signifies a line should be skipped.
Attributes
----------
_filename : str
the name of the file.
_data : list of string
the contents of the file, by line
_delimiter : str
the name of the file.
_end_of_line_comment_char : str
end-of-line comment character to be ignored.
_full_line_comment_char : str
comment character that signifies a line should be skipped.
_current_row : int
the current row of the file.
_anchored : bool
indicator that position is relative to a landmark location.
"""
def __init__(self, end_of_line_comment_char=None, full_line_comment_char=None):
"""
Initialize attributes.
"""
self._filename = None
self._data = []
self._delimiter = " \t"
self._end_of_line_comment_char = end_of_line_comment_char
self._full_line_comment_char = full_line_comment_char
self._current_row = 0
self._anchored = False
self.set_delimiters(self._delimiter)
def set_file(self, filename):
"""
Set the name of the file that will be generated.
Parameters
----------
filename : str
Name of the input file to be generated.
"""
self._filename = filename
inputfile = open(filename, 'r')
if not self._end_of_line_comment_char and not self._full_line_comment_char:
self._data = inputfile.readlines()
else:
self._data = []
for line in inputfile:
if line[0] == self._full_line_comment_char:
continue
self._data.append(line.split(self._end_of_line_comment_char)[0])
inputfile.close()
def set_delimiters(self, delimiter):
r"""
Set the delimiters that are used to identify field boundaries.
Parameters
----------
delimiter : str
A string containing characters to be used as delimiters. The
default value is ' \t', which means that spaces and tabs are not
taken as data but instead mark the boundaries. Note that the
parser is smart enough to recognize characters within quotes as
non-delimiters.
"""
self._delimiter = delimiter
if delimiter != "columns":
ParserElement.setDefaultWhitespaceChars(str(delimiter))
self._reset_tokens()
def mark_anchor(self, anchor, occurrence=1):
"""
Mark the location of a landmark, which lets you describe data by relative position.
Note that a forward search begins at the old anchor location. If you want to restart
the search for the anchor at the file beginning, then call ``reset_anchor()`` before
``mark_anchor``.
Parameters
----------
anchor : str
The text you want to search for.
occurrence : int
Find nth instance of text; default is 1 (first). Use -1 to
find last occurrence. Reverse searches always start at the end
of the file no matter the state of any previous anchor.
"""
if not isinstance(occurrence, int):
raise ValueError("The value for occurrence must be an integer")
instance = 0
if occurrence > 0:
count = 0
max_lines = len(self._data)
for index in range(self._current_row, max_lines):
line = self._data[index]
# If we are marking a new anchor from an existing anchor, and
# the anchor is mid-line, then we still search the line, but
# only after the anchor.
if count == 0 and self._anchored:
line = line.split(anchor)[-1]
if anchor in line:
instance += 1
if instance == occurrence:
self._current_row += count
self._anchored = True
return
count += 1
elif occurrence < 0:
max_lines = len(self._data) - 1
count = max_lines
for index in range(max_lines, -1, -1):
line = self._data[index]
# If we are marking a new anchor from an existing anchor, and
# the anchor is mid-line, then we still search the line, but
# only before the anchor.
if count == max_lines and self._anchored:
line = line.split(anchor)[0]
if anchor in line:
instance += -1
if instance == occurrence:
self._current_row = count
self._anchored = True
return
count -= 1
else:
raise ValueError("0 is not valid for an anchor occurrence.")
raise RuntimeError("Could not find pattern %s in output file %s" %
(anchor, self._filename))
def reset_anchor(self):
"""
Reset anchor to the beginning of the file.
"""
self._current_row = 0
self._anchored = False
def transfer_line(self, row):
"""
Return an entire line, relative to current anchor.
Parameters
----------
row : int
Number of lines offset from anchor line (0 is anchor line).
This can be negative.
Returns
-------
string
Line at the location requested.
"""
return self._data[self._current_row + row].rstrip()
def transfer_var(self, row, field, fieldend=None):
"""
Get a single variable relative to the current anchor.
Parameters
----------
row : int
Number of lines offset from anchor line (0 is anchor line).
This can be negative.
field : int
If the delimiter is a set of chars: which word in line to retrieve.
If the delimiter is 'columns': character position to start.
fieldend : int (optional)
If the delimiter is a set of chars: IGNORED.
If the delimiter is 'columns': position of last character to return, or if
omitted, the end of the line is used.
Returns
-------
string
Data from the requested location in the file.
"""
j = self._current_row + row
line = self._data[j]
if self._delimiter == "columns":
if not fieldend:
line = line[(field - 1):]
else:
line = line[(field - 1):(fieldend)]
# Let pyparsing figure out if this is a number, and return it
# as a float or int as appropriate
data = self._parse_line().parseString(line)
# data might have been split if it contains whitespace. If so,
# just return the whole string
if len(data) > 1:
return line
else:
return data[0]
else:
data = self._parse_line().parseString(line)
return data[field - 1]
def transfer_keyvar(self, key, field, occurrence=1, rowoffset=0):
"""
Search for a key relative to the current anchor and get a field from that line.
You can do the same thing with a call to ``mark_anchor`` and ``transfer_var``.
This function just combines them for convenience.
Parameters
----------
key : str
The key to search for.
field : int
Which field to transfer. Field 0 is the key.
occurrence : int
Find nth instance of text; default is 1 (first value
field). Use -1 to find last occurance. Position 0 is the key
field, so it should not be used as a value for occurrence.
rowoffset : int (optional)
Optional row offset from the occurrence of key. This can
also be negative.
Returns
-------
string
Data from the requested location in the file.
"""
if not isinstance(occurrence, int) or occurrence == 0:
msg = "The value for occurrence must be a nonzero integer"
raise ValueError(msg)
instance = 0
if occurrence > 0:
row = 0
for line in self._data[self._current_row:]:
if line.find(key) > -1:
instance += 1
if instance == occurrence:
break
row += 1
elif occurrence < 0:
row = -1
for line in reversed(self._data[self._current_row:]):
if line.find(key) > -1:
instance += -1
if instance == occurrence:
break
row -= 1
j = self._current_row + row + rowoffset
line = self._data[j]
fields = self._parse_line().parseString(line.replace(key, "KeyField"))
return fields[field]
def transfer_array(self, rowstart, fieldstart, rowend=None, fieldend=None):
"""
Get an array of variables relative to the current anchor.
Setting the delimiter to 'columns' elicits some special behavior
from this method. Normally, the extraction process wraps around
at the end of a line and continues grabbing each field at the start of
a newline. When the delimiter is set to columns, the parameters
(rowstart, fieldstart, rowend, fieldend) demark a box, and all
values in that box are retrieved. Note that standard whitespace
is the secondary delimiter in this case.
Parameters
----------
rowstart : int
Row number to start, relative to the current anchor.
fieldstart : int
Field number to start.
rowend : int, optional
Row number to end. If not set, then only one row is grabbed.
fieldend : int
Field number to end.
Returns
-------
string
Data from the requested location in the file.
"""
j1 = self._current_row + rowstart
if rowend is None:
j2 = j1 + 1
else:
j2 = self._current_row + rowend + 1
if not fieldend:
raise ValueError("fieldend is missing, currently required")
lines = self._data[j1:j2]
data = np.zeros(shape=(0, 0))
for i, line in enumerate(lines):
if self._delimiter == "columns":
line = line[(fieldstart - 1):fieldend]
# Stripping whitespace may be controversial.
line = line.strip()
# Let pyparsing figure out if this is a number, and return it
# as a float or int as appropriate
parsed = self._parse_line().parseString(line)
newdata = np.array(parsed[:])
# data might have been split if it contains whitespace. If the
# data is string, we probably didn't want this.
if newdata.dtype.type is np.str_:
newdata = np.array(line)
data = np.append(data, newdata)
else:
parsed = self._parse_line().parseString(line)
if i == j2 - j1 - 1:
data = np.append(data, np.array(parsed[(fieldstart - 1):fieldend]))
else:
data = np.append(data, np.array(parsed[(fieldstart - 1):]))
fieldstart = 1
return data
def transfer_2Darray(self, rowstart, fieldstart, rowend, fieldend=None):
"""
Get a 2D array of variables relative to the current anchor.
Each line of data is placed in a separate row.
If the delimiter is set to 'columns', then the values contained in
fieldstart and fieldend should be the column number instead of the
field number.
Parameters
----------
rowstart : int
Row number to start, relative to the current anchor.
fieldstart : int
Field number to start.
rowend : int
Row number to end relative to current anchor.
fieldend : int (optional)
Field number to end. If not specified, grabs all fields up to the
end of the line.
Returns
-------
string
Data from the requested location in the file.
"""
if fieldend and (fieldstart > fieldend):
msg = "fieldend must be greater than fieldstart"
raise ValueError(msg)
if rowstart > rowend:
msg = "rowend must be greater than rowstart"
raise ValueError(msg)
j1 = self._current_row + rowstart
j2 = self._current_row + rowend + 1
lines = list(self._data[j1:j2])
if self._delimiter == "columns":
if fieldend:
line = lines[0][(fieldstart - 1):fieldend]
else:
line = lines[0][(fieldstart - 1):]
parsed = self._parse_line().parseString(line)
row = np.array(parsed[:])
data = np.zeros(shape=(abs(j2 - j1), len(row)))
data[0, :] = row
for i, line in enumerate(list(lines[1:])):
if fieldend:
line = line[(fieldstart - 1):fieldend]