How to get rid of the "n" at the end of each line while writing to a variable?

Question:

I have the following code to read data

import sys

data = sys.stdin.readlines()
id = 0

while id < len(data) - 1:
    n = int(data[id])
    id += 1
    some_list = []
    for _ in range(n):
        x1, y1, x2, y2 = map(str, data[id].split(" "))
        some_list.append([x1, y1, x2, y2])
        id += 1
    print(some_list)

Input:

2
0 3 1 2
2 1 3 1
4
3 1 1 0
0 0 2 1
1 1 2 0
3 0 3 1

Its output:

[['0', '3', '1', '2n'], ['2', '1', '3', '1n']]
[['3', '1', '1', '0n'], ['0', '0', '2', '1n'], ['1', '1', '2', '0n'], ['3', '0', '3', '1']]

You can see that "n" is also written.
How can I ignore "n" (or remove it) without losing data read speed?

I need numbers to remain in string format. The construction sys.stdin.readlines() is also needed since I don’t know how many lines (how many m-s) will be in the input.

Asked By: niico

||

Answers:

You may use rstrip from the string package.

For example here, just use:

y2.rstrip()

It will remove the n at the end of y2 if there is one.

Answered By: Asthom
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.