0

I want to input a set of string separated with linefeed character and i want to use sys.stdin.read() but i'm confuse how it works.

This is what i've tried:

import re
import sys

string = sys.stdin.read()
for i in string:
    thearitmethic = re.findall('[\d\(\)\+\-\*\/\.]', i)
    print(thearitmethic)
Grysella
  • 3
  • 3
  • Side-note: [Always use raw strings for your regular expressions](https://stackoverflow.com/q/12871066/364696); the backslashes don't behave reliably when you don't (some of them get interpreted as ASCII string escapes, others as actual backslashes followed by the following character). In this case, none of them are ASCII escapes, so making it raw with a leading `r` (`r'[\d\(\)\+\-\*\/\.]'`, or, given the backslashes can be avoided in character classes, `r'[-\d()+*/.]'`) doesn't change behavior, though it will avoid the warnings you'd see if you enabled warnings when running your code. – ShadowRanger Jun 09 '21 at 17:00

1 Answers1

0

The read() call consumes all input until EOF into a single string. While you then could split it on newlines, it's probably better to just read one line at a time.

for line in sys.stdin:
    print(re.findall(r'[-\d()+*/.]', line))

Notice also the r'...' raw string to pass backslashes through to the regex engine. However, there is no need to backslash-escape dots, curly parenteses etc inside a [...] character class.

The problem with reading everything into a single string is that you are unnecessarily keeping potentially large amounts of data in memory. Your loop body only needs to see one line at a time; don't keep more than that in memory at any one time. (You could still in theory exhaust your RAM by reading a really long single line, but this is considered obscure enough to not worry many people in actual practice.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Just a minor note: If they don't want the newlines in the line, just add `line = line.rstrip('\n')` between your two lines above to pre-strip the newline. – ShadowRanger Jun 09 '21 at 17:04
  • The regex inside the loop does not extract newlines anyway; but sure, there are other use cases where you definitely want to remove the trailing newline or do other preprocessing at the top of the loop body. – tripleee Jun 09 '21 at 17:11