1

I want to get an input from the console that is a text with several lines. Since I want to do it all in a single input I have to mark '\n' in the middle to signal the different lines. After I get the input I want to save the text in a matrix where each line is a line from the text with the words separated.

This is my function to do so:

def saveText(text):
    text = text[6:len(text)-6]
    line = 0
    array = [[]]
    cur = ""
    for i in range (len(text)):
        if (text[i] == '\n'):
            line+=1
            array.append([])
        else:
            if ((text[i] == ' ' or text[i] == ',' or text[i] == ';' or text[i] == '.') and cur != ""):
                array[line].append(cur)
                cur = ""
            else:
                cur += text[i]
    return array

However, when I print the variable array it appears as a matrix with only one line, and besides the '\n' are counted as words, they also appear as '\n'.

Can anyone help me with this?

  • I actually use the print() function, but either way I'm printing the array created with the input so the problem is probably there... – Toca Fonseca Mar 13 '20 at 17:55
  • 1
    When you print a list, you get the escaped representation of newlines. So, `print(["\n"])` gives you `['\n']` even though that's really a one character new line. If you print each element of the list you get the real newline, which looks like a blank line on your screen. – tdelaney Mar 13 '20 at 17:59
  • Something like `list(itertools.chain.from_iterable(x.split(" ,;.") for x in io.StringIO(text[6:-6])))` would probably be simpler and faster than iterating over the text character by character. – chepner Mar 13 '20 at 18:02
  • But the thing is, on the function I try to filter all the characters like ',' ';' '.' and '\n'... That newline is not even supposed to be there. My goal is that the final matrix has only words and not any special characters. It really "filters" them all, except the newline – Toca Fonseca Mar 13 '20 at 18:02
  • FYI: a shortcut `if text[i] in ' .,;' and cur:` – Jongware Mar 13 '20 at 18:19

2 Answers2

1

You didn't provide an input string to test with, so I just made my own. You can use .split() to split on new lines and spaces (or anything else that you want).

Edit: I think I understand what you mean now. I think you are trying to have the user input newline characters \n when asking them for input. This isn't possible using input, but there is a workaround. I integrated the answer from that link into the code below.

If you instead wanted the user to manually write \n when getting input from them, then you'd need to change text.splitlines() to text.split('\\n'). You could also replace\nwith\nby usingtext.replace('\n', '\n')`.

But, I think it'd be less error prone to just use the multi-line input as shown below and discussed further in like above.

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break

input_text = '\n'.join(lines)

def save_text(text):
    lines = text.splitlines()
    matrix = []
    for line in lines:
        matrix.append(line.split(' '))
    return matrix

print(save_text(input_text))

Input from user looks like this:

hello how
are you
doing this fine
day?

outputs:

[['hello', 'how'], ['are', 'you'], ['doing', 'this', 'fine'], ['day?']]
Stuart
  • 465
  • 3
  • 6
  • I just got a chance to test it now and it still counts the '\n' as '\\n', I could solve it but only changing the '\n' in the line 4 of your code by '\\n'... Is it because that I'm actually writing '\n' in my input that it reads as '\\n'? – Toca Fonseca Mar 14 '20 at 08:48
  • @TocaFonseca the only mention of `\\n` was in the title so it wasn't clear to me what that was about. You should provide example input text with the `\\n`. Basically, `\` works as an escape character. So `\\n` is not interpreted as a newline character, but instead just a literal string of `\\n`. Provide some example input text or the code you're using to get the input, and I'll update the answer. – Stuart Mar 14 '20 at 14:04
  • @TocaFonseca I think I misunderstood your original question. I edited the answer above. – Stuart Mar 14 '20 at 18:20
1
text = "line1: hello wolrd\nline2: test\nline2: i don't know what to write"
lines = [x.split() for x in text.split("\n")]
print(lines)

This will return:

[['line1:', 'hello', 'wolrd'], ['line2:', 'test'], ['line2:', 'i', "don't", 'know', 'what', 'to', 'write']]

the idea is the same as Stuart's solution, it's just a little bit more efficient.

rémi couturier
  • 425
  • 3
  • 11