10 Replies - 1159 Views - Last Post: 10 May 2018 - 08:15 AM Rate Topic: -----

#1 bobsmith76   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 14-February 17

Input strings into command line with carriage returns

Posted 05 April 2018 - 06:22 AM

Suppose you have a list of strings which are separated with a carriage return and you want to input them into command line such that the program will take in all of the strings. Right now I have to eliminate the carriage return before I input the string but I want to be able to input all of the strings without having to delete the carriage return. For example, suppose you have the strings:

1. Hey you
2. What's up.
3. Nothing much, how about you?
4. Same old, same old.

I want to be able to copy and paste all 4 of those strings into a command line which uses the input prompt. Right now, if I try to input them into an input prompt then the program will only process the first line.

Is This A Good Question/Topic? 0
  • +

Replies To: Input strings into command line with carriage returns

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14238
  • View blog
  • Posts: 57,114
  • Joined: 12-June 08

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 06:23 AM

Why wouldn't your app just ask for input when it starts?
Was This Post Helpful? 0
  • +
  • -

#3 bobsmith76   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 14-February 17

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 06:54 AM

View Postmodi123_1, on 05 April 2018 - 06:23 AM, said:

Why wouldn't your app just ask for input when it starts?


It does but because those strings are separated by carriage returns it only processes the first string before the carriage return.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14238
  • View blog
  • Posts: 57,114
  • Joined: 12-June 08

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 06:59 AM

Sounds like a language specific issue. What language and I can move this to the forum.
Was This Post Helpful? 0
  • +
  • -

#5 bobsmith76   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 14-February 17

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 07:39 AM

View Postmodi123_1, on 05 April 2018 - 06:59 AM, said:

Sounds like a language specific issue. What language and I can move this to the forum.


Python.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6328
  • View blog
  • Posts: 21,733
  • Joined: 05-May 12

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 09:46 AM

This also sounds like a question regarding getting console I/O, not command line parameters.
Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 420
  • View blog
  • Posts: 1,341
  • Joined: 27-December 13

Re: Input strings into command line with carriage returns

Posted 05 April 2018 - 01:15 PM

Sounds like you need the strip() method.

For the example I use a small notepad file, string.txt:
line1
line2
line3
line4



If you save this file and run the code below, I guess you get what you are looking for..?:
txt = []
with open("string.txt") as file:
    for line in file:
        txt.append(line)  # a simple append of line data
print(txt)


txt = []
with open("string.txt") as file:
    for line in file:
        txt.append(line.strip())  # use of the strip method to remove leading and trailing whitespace ( including cr)
print(txt)

Was This Post Helpful? 0
  • +
  • -

#8 bobsmith76   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 14-February 17

Re: Input strings into command line with carriage returns

Posted 06 April 2018 - 02:20 AM

What you're saying is that I need to alter the text I'm working on before I enter it into command line. I think that's probably the only thing that can be done. I'm pretty sure command line is coded to run as soon as it encounters a carriage return.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,715
  • Joined: 12-December 12

Re: Input strings into command line with carriage returns

Posted 06 April 2018 - 02:33 AM

Can you clarify, are you talking about command line options, or using the input dialog once your program has started?

If the input dialog, then you can receive multiple inputs by placing the input within a loop, breaking out when the user provides an empty value (or some other value to indicate completion).

If for command line arguments, then you could replace the newline characters with some other delimiter, such as a comma.
Was This Post Helpful? 0
  • +
  • -

#10 bobsmith76   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 14-February 17

Re: Input strings into command line with carriage returns

Posted 10 May 2018 - 07:18 AM

the eventual workaround I chose was something like DK3250's solution. I just input the sentences into a text file, then run the command line, then program the app to input the sentences from the text file.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11126
  • View blog
  • Posts: 19,070
  • Joined: 19-March 11

Re: Input strings into command line with carriage returns

Posted 10 May 2018 - 08:15 AM

If you're doing this in a UNIX-like enviroment, one standard pattern would be to take input from STDIO. Then you could do

$ python my_program.py < string.txt


to read from that file, or this

$ python my_program.py
line1
line2
line3
line4
^D


if you want to enter lines from the keyboard.

Some programs offer the convenience of assuming the first argument, if given, is a filename, which allows you to omit the < (for example cat works this way)

By way of an example, here's a simple script that just provides command-line access to a prototype object that I'm working on at the moment:

# Script for allocation a set of input judges to a set of input applications.
# Syntax:
# python3 allocate.py <csv-file>
# CSV file should be as created by generate.py

import sys

from app_allocator.classes.allocator import Allocator

filepath = None
heuristic = ""

if len(sys.argv) > 1:
    filepath = sys.argv[1]
if len(sys.argv) > 2:
    heuristic = sys.argv[2]


allocator = Allocator(filepath, heuristic)
allocator.read_entities()
allocator.setup()
allocator.allocate()
allocator.assess()



Don't worry too much about what this is for or what it does. What you can see is that we want to let the user set one or two parameters and then we use those parameters to instantiate an object and perform a few actions. The last action, assess(), produces output, which can be and usually is redirected to a file in the usual way.
You may assume that the Allocator object is written to handle the missing objects in a graceful fashion. In fact, what happens is this:
    def _file(self):
        if self.filepath is None:  # pragma: nocover
            return sys.stdin  # pragma: nocover
        else:
            return open(self.filepath)  # pragma: nocover

    def read_entities(self):
        with self._file() as file:
            # read the file (details omitted)




and the "heuristic" defaults to a simple randomized selector if nothing is specified on the command line. (since this is a prototype, we're not spending a lot of effort handling stuff like bad filenames - we just fail in that case)

You should probably play with some of the more standard UNIX utilities to see how they fit together, which will help you figure out how you want these interfaces to work. Eric Raymond's book on The Art Of UNIX Programming has some useful insights on this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1