-2

I'm trying to write code that receives two texts and thereafter I am writing code that finds common words among the texts. This is put in a new list. sys.stdin.read() is being used instead of input() because I need to process a long piece of text.

Below is what I wrote thus far. When I run the code it seems to hang because its only asking for input for text1 and never reaches text 2 input request.

What is going on and how can I fix it?

size text 1 = approx. 500.000 chars.

size text 2 = approx. 500.000 chars.

import sys

text1 = sys.stdin.read()
print(text1)
    
 text2 = sys.stdin.read()
 print(text2)

# ... snippet ... compare code here ...
ZF007
  • 3,708
  • 8
  • 29
  • 48
Aleshka
  • 11
  • 2
  • For the record, this correctly asks for two strings for me. – Carcigenicate Oct 24 '20 at 23:26
  • Did you press ENTER? :-) – jurez Oct 24 '20 at 23:28
  • sys.stdin.read() has a bit different purpose. Check the docs. You need to specify how many characters you'd like to read to use it for your case. For example, `sys.stdin.read(4)` will read the first 4 entered characters. Overall, it's better to use `input()` for your case. Also, check https://stackoverflow.com/questions/6055659/input-vs-sys-stdin-read – Max Oct 24 '20 at 23:28
  • `sys.stdin.read()` reads everything you typed. It has no way of telling that the first text ended and the second started. – DYZ Oct 25 '20 at 00:48
  • Well,not reads until and EOF – juanpa.arrivillaga Oct 25 '20 at 01:02
  • @jurez i pressed ctrl+d – Aleshka Oct 26 '20 at 12:08
  • @MaxTsybanov im using sys.stdin.read insted of input because i want to input a long text, an article. – Aleshka Oct 26 '20 at 12:08
  • @DYZ isnt ctrl+d telling to end the input process? – Aleshka Oct 26 '20 at 12:08
  • .. if needed correct character length for each text in your question. These specifics are missing. BTW.. I've got a solution but the question needs to be reopend first for which I filed request. – ZF007 Oct 26 '20 at 17:20
  • @ZF007 hey received a massage that the question reopened. I would glad if you could write your solution. – Aleshka Oct 26 '20 at 19:01

2 Answers2

0

I think this will work

text1 = input("Input some text: ")
text2 = input("Input some text: ")

I don't actually know whats going wrong in yours thoough

  • hi man, im using **sys.stdin.read** insted of **input** because i want to input a long text, an article. – Aleshka Oct 26 '20 at 12:01
0

In the code below you have two different type of input(). At text-1 you add unlimited amount of text, press enter, then type "EOF!' and press enter to go to input for text 2. At text-2 you have the input() bound to the amount of characters (likely to be ASCII or UTF-8 type; accepts all types as written for now). When 500.000 characters is received text-2 is done and you can go to the compare step, which you might have written by now. If that gives problems too post a new question for that.

The print statements are there to show you what the input per text is minus "EOF!" marker for text-1. It can be any type of marker but End Of File seemed to be obvious for now to explain the stop-marker.

import sys

text1 = ''
text2 = ""
nr_lines = 2

while True:
    text1 += input()+'\n'
#    print(text1[-5:-1])
    if text1[-5:-1] == 'EOF!':
        break

print(f'\nlen: {len(text1)}, text1 {text1}\n\n'[:-7])

while True:
    text2 = input()
    if len(text2) >= 500000:
        break

print(f'\nlen: {len(text2)}, text2 {text2}\n\n')

The reason why sys.stdin.read() is not working is due to a different purpose and therefore the lack of functionality written into the method. Its one direction communication and not bi-directional.

ZF007
  • 3,708
  • 8
  • 29
  • 48