-1

There is a translation table (dictionary) above this code.

Synopsis of the code:

Converting/translating a string of characters to another character in a different ASCII/Unicode-8 character and printing this output to a new file with a user inputted name.

The input of this conversion should be a user input and should accept multiline string.

print("Enter PC2 text to convert to InPage: ")

text = sys.stdin.read()
translated = text.maketrans(translation)
finaltext = text.translate(translated)

print("Provide a name of the file:")
fileofname = input()
name = fileofname+'.txt'

with open(name, 'w') as f:
    sys.stdout = f
    f.write(finaltext)

I have tried the following ways to overcome this issue and none of them have worked out the way I intent it to:

  1. import sys
  2. while looping
  3. raw_input (python 2)
  4. EOF
BotZakk
  • 29
  • 5
  • 1
    `sys.stdin.read()` will keep reading until you type the EOF character (Ctl-d on Unix). So that will accept multiline input. – Barmar May 13 '23 at 14:41
  • Does this answer your question? [Multiline user input python](https://stackoverflow.com/questions/17016240/multiline-user-input-python) – SimonUnderwood May 13 '23 at 14:42
  • Prompted with a EOFErorr: ``` Traceback (most recent call last): File "C:\Users\DELL\PycharmProjects\pythonProject\PC2 to InPage.py", line 48, in fileofname = input() ^^^^^^^ EOFError: EOF when reading a line ``` – BotZakk May 13 '23 at 14:43
  • 1
    `text = "".join(sys.stdin.readlines())` should work, using ctrl-D or ctrl-Z to finish – Alain T. May 13 '23 at 15:00

2 Answers2

0
import sys

// print("Enter PC2 text to convert to InPage:")
// text = input()

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

text = ''.join(lines)

translated = text.maketrans(translation)
finaltext = text.translate(translated)

print("Provide a name for the file:")
fileofname = input()
name = fileofname+'.txt'

with open(name, 'w') as f:
    sys.stdout = f
    f.write(finaltext)

please note, in python 3, you should use input() instead of raw_input() for user input

bramasta vikana
  • 274
  • 1
  • 12
0

You could loop on a regular input() in an infinite loop that you let the user break with ctrl-C:

text = ""
while True:
    try:   text += input() + "\n"
    except KeyboardInterrupt: break
Alain T.
  • 40,517
  • 4
  • 31
  • 51