raw_input accepts any input up until a new line character is entered.
The easiest way to do what you are asking it to accept more entries, until an end of file is encountered.
print("please copy and paste your charge discharge data.\n"
"To end recording Press Ctrl+d on Linux/Mac on Crtl+z on Windows")
lines = []
try:
while True:
lines.append(raw_input())
except EOFError:
pass
lines = "\n".join(lines)
Then do something with the whole batch of text.
Answer from user764357 on Stack OverflowHi, im making a text sorter, but i want to be able to copy and paste multiple lines of text into the input, so:
firstname, lastname
firstname2, lastname2
firstname3, lastname3
.....
i print it out after it runs, and its blank
filename = (list)(input("List: \33[37m"))
textFile = open("Names.txt", "w")for line in filename:textFile.write(filename[line])textFile = open("Names.txt", "r")print(textFile.read())
is there any way to do this? i need to get that list of names to be able to be converted to a text file so I can do things with it
Edit: for clarification, I want to be able to do multiple lines with one copy/paste. and the way that the text is currently formatted, it doesn’t have \n included after the end of each line.
I am given a large text document full of names, and I need to copy paste them in and organize them from there
EDIT 2: SOLUTION FOUND
I found this on the internet, and it worked perfectly for me!! https://www.csestack.org/multiline-user-input-in-python/
Copy/pasting: multiline in a single line prompt
Allow pasting multiple lines in the Python Console
string - Multiple lines user input in command-line Python application - Stack Overflow
[Feature] Be able to paste multiple lines at once at the >>> prompt
Probably not the most beautiful procedure, but this works:
cmds = '''
paste your commands, followed by ''':
a = 1
b = 2
c = 3
'''
Then exec(cmds) will execute them.
Or more directly,
exec('''
then paste your commands, followed by '''):
a = 1
b = 2
c = 3
''')
It's just a trick, maybe there's a more official, elegant way.
IdleX provides the PastePyShell.py extension for IDLE which allows pasting of multiple lines into the shell for execution.
import sys
text = sys.stdin.read()
After pasting, you have to tell python that there is no more input by sending an end-of-file control character (ctrl+D in Linux, ctrl+Z followed by enter in Windows).
This method also works with pipes. If the above script is called paste.py, you can do
$ echo "hello" | python paste.py
and text will be equal to "hello\n". It's the same in windows:
C:\Python27>dir | python paste.py
The above command will save the output of dir to the text variable. There is no need to manually type an end-of-file character when the input is provided using pipes -- python will be notified automatically when the program creating the input has completed.
You could get the text from clipboard without any additional actions which raw_input() requires from a user to paste the multiline text:
import Tkinter
root = Tkinter.Tk()
root.withdraw()
text = root.clipboard_get()
root.destroy()
See also How do I copy a string to the clipboard on Windows using Python?
I'm trying to feed a prompt to a GPT-2 model, which is trained on song lyrics. With this model, you can feed it a prompt and it will attempt to finish it. The problem I'm having is that I need for the prompt to be formatted correctly like song lyrics - with each lyric on a newline. Since this model has been trained on formatted lyrics, the prompt needs to look the same to produce above average results.
input() doesn't work, because hitting return submits the text. I've also tried sys.stdin.read() as well and nothing seems to happen, just returns ' '.
Is it because I'm running it in a notebook? I'm only like 8 months deep into this python hobby, so I could be asking for something that can't happen.
Here's the code, would appreciate any ideas you've got.
prompt = input('Input Prompt:')
gpt2.generate(sess,
length=200,
temperature=.99,
prefix= prompt, #this is where the prompt goes
nsamples=5,
top_p=.9,
batch_size=5,
run_name='run1'
)
To take multiple lines of input and put them in a list of strings, you can use sys.stdin.readlines() which will keep accepting returns until you give it an EOF [ctrl + d in *nix, ctrl + z in Windows].
E.g.:
user_input = sys.stdin.readlines("Favourite foods followed by EOF:") # User gives input...
print(user_input)
Output:
>> ["Spam\n", "Spam\n", "Spam\n"]
Obviously this leaves you with newlines, so you could do something hacky like:
user_input = [line.rstrip() for line in sys.stdin.readlines()]
Which will give you:
>> ["Spam", "Spam", "Spam"]
Sounds like an interesting ML problem, though dealing with a list of strings may complicate things significantly. You might want to make them one long string using something like join, e.g.
" ".join(user_input) # Used with a separator of " ", but you could use newlines or something
Which gives one string of:
>> "Spam Spam Spam"
Can't you use a while loop to read the input? e.g.
prompt = []
line = input("Input prompt ending with an empty line: ")
while line:
prompt.append(line)
line = input()
prompt = "\n".join(prompt)