rcslice - 1-Indexed List Slicing
rcslice is a Python library (version 1.1.0) designed to slice lists of 'sliceables' using a 1-indexed system where both start and end indices are inclusive. This utility helps in specific use cases, such as slicing file content line by line or column by column, offering an alternative to Python's native 0-indexed, end-exclusive slicing. It appears to be a stable, focused utility with infrequent releases.
Common errors
-
IndexError: list index out of range
cause Attempting to slice with 0-based indexing (e.g., '0-2' for the first three items) or requesting an index beyond the actual 1-based size of the list.fixRemember that `rcslice` uses 1-based indexing. For the first item, use '1'. For a list of 5 items, the last valid index is '5'. Ensure your slice range (e.g., '1-5') aligns with the 1-indexed length of your data. -
Output list contains fewer or more elements than expected, or includes unexpected separator lines.
cause Misunderstanding the inclusive nature of the end index or the automatic separator insertion for multiple slices.fixIf slicing '1-3', it includes elements at 1, 2, and 3. If combining slices (e.g., '1-2,4'), ensure `RowSlice` is instantiated with the desired separator behavior (e.g., `RowSlice([])` for no separator, `RowSlice(['---'])` for a visual separator). -
AttributeError: 'list' object has no attribute 'split' or similar string manipulation error.
cause The `rcslice` library expects each item in the input list to be a 'sliceable' (e.g., a string that can be split for column operations). If you pass a list of non-string objects or objects that don't support `split`, column operations will fail.fixEnsure that if you are performing column-based slicing (e.g., '1.2'), the items within your input list are strings or objects that correctly implement string-like `split()` behavior.
Warnings
- gotcha rcslice uses 1-indexed slicing where both the start and end indices are *inclusive*, directly contrasting Python's native 0-indexed, end-exclusive slicing. This is the most common source of 'off-by-one' errors and unexpected results.
- gotcha When using multiple slice syntaxes (e.g., '1-2,4-5'), `rcslice` will insert a separator between the results of each individual slice operation. If not specified during `RowSlice` instantiation (e.g., `rs = RowSlice([''])`), the default behavior might lead to unintended formatting or empty lines in the output.
- gotcha The primary class is `RowSlice`, and its documentation emphasizes a 'special priority on row'. While column slicing is supported (e.g., '1.2-3.4'), complex interactions between row and column specifications in the slicing syntax may behave non-intuitively if not thoroughly understood from the documentation.
Install
-
pip install rcslice
Imports
- RowSlice
from rcslice import RowSlice
Quickstart
from rcslice import RowSlice
# Create a list of sliceable items
data = [
"Header 1,Header 2,Header 3",
"Row 1 Col 1,Row 1 Col 2,Row 1 Col 3",
"Row 2 Col 1,Row 2 Col 2,Row 2 Col 3",
"Row 3 Col 1,Row 3 Col 2,Row 3 Col 3",
"Row 4 Col 1,Row 4 Col 2,Row 4 Col 3"
]
# Initialize RowSlice (no separator for single slice operations)
rs = RowSlice()
# Example 1: Slice rows 1 to 3 (inclusive)
# Input '1-3' means first element to third element
print("\n--- Slicing rows 1-3 ---")
sliced_rows = rs.slice(data, '1-3')
for item in sliced_rows:
print(item)
# Example 2: Slice row 2, column 2 (1-indexed)
print("\n--- Slicing Row 2, Column 2 ---")
sliced_single_cell = rs.slice(data, '2.2')
for item in sliced_single_cell:
print(item)
# Example 3: Multiple slices with a custom separator
# Slices: row 1-2, then row 4
# The separator ['---'] means a single '---' line will be inserted between the slices.
rs_with_separator = RowSlice(['---'])
print("\n--- Multiple Slices with Separator (rows 1-2, then row 4) ---")
multi_sliced_data = rs_with_separator.slice(data, '1-2,4')
for item in multi_sliced_data:
print(item)