2

I'm currently working on a simple script where i want to take the input from the console. This input is a python code snippet. While writing the input in the file, overall alignment is improper.

For an eg:

model_def = str(input("Paste the defination of the classifier used :"))
f = open("classifier.py","w+")
f.write(model_def)
f.close()

Input was provided something like that:

class classifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(2208, 500)
        self.fc2 = nn.Linear(500, 256)
        self.fc3 = nn.Linear(256, 3)
        self.dropout = nn.Dropout(0.5)
        self.logsoftmax = nn.LogSoftmax(dim=1)
        self.acivation = relu
    def forward(self,x):
        x = x.view(x.size(0), -1)
        x = self.dropout(self.acivation(self.fc1(x)))
        x = self.dropout(self.acivation(self.fc2(x)))

        x = self.logsoftmax(self.fc3(x))
        return x

Saving it results in an improper alignment.

class classifier(nn.Module):     def __init__(self):         super().__init__()         self.fc1 = nn.Linear(2208, 500)         self.fc2 = nn.Linear(500, 256)         self.fc3 = nn.Linear(256, 3)         self.dropout = nn.Dropout(0.5)         self.logsoftmax = nn.LogSoftmax(dim=1)         self.acivation = relu     def forward(self,x):         x = x.view(x.size(0), -1)         x = self.dropout(self.acivation(self.fc1(x)))         x = self.dropout(self.acivation(self.fc2(x)))          x = self.logsoftmax(self.fc3(x))         return x
micharaze
  • 957
  • 8
  • 25
Ashish Bairwa
  • 757
  • 1
  • 6
  • 19
  • Explore readlines() from the inbuilt sys library. It might be of some help to you. – Dipesh Poudel Aug 22 '19 at 06:59
  • 2
    probably related: https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-user – mikuszefski Aug 22 '19 at 07:03
  • Possible duplicate of [Python 3: receive user input including newline characters](https://stackoverflow.com/questions/2542171/python-3-receive-user-input-including-newline-characters) – asikorski Aug 22 '19 at 10:05

3 Answers3

1

You have to read from input until EOF. You can raise EOF with Ctrl-D or Ctrl-Z (windows):

Python 3

print("Paste the defination of the classifier used. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
    try:
        line = input()
    except EOFError:
        break
    contents.append(line)

f = open("classifier.py","w+")
for line in contents:
    f.write("%s\n" % line)
f.close()
micharaze
  • 957
  • 8
  • 25
  • Got your point. So i have to read and write it line by line... Isn't there any other method by which i can recieve this thing directly – Ashish Bairwa Aug 22 '19 at 07:16
  • Not if you want to paste text with newlines. You can add separators like a \ instead of newlines in our pasted code and parse them, but I don't think that this is what you want – micharaze Aug 22 '19 at 07:21
0

Try this one:

lines = []
line = input()
while line:
    lines.append(line)
    line = input()
print(lines)

Inputting will break with first empty line. As a result you will get list of lines then you can break them up however you prefer. E.g. windows:

lines_str = "\r\n".join(lines)

Ref: https://www.reddit.com/r/Python/comments/2y6f7x/reading_multiple_lines_of_input/

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
  • Look at his example input, there is a blank line. Your code stops on a blank line as input. – micharaze Aug 22 '19 at 07:46
  • yup, I was thinking about something what you won't have to explicitly kill by keyboard combination. But indeed - that might be some downside, depending on the character of input ... – Grzegorz Skibinski Aug 22 '19 at 07:53
0

You might use sys.stdin.read() instead of input:

import sys

data = sys.stdin.read()

Hit Ctrl+D/Ctrl+Z when you're done with providing input.

asikorski
  • 882
  • 6
  • 20