How to process raw data (in python)? [closed]

2019-09-09 03:25发布

问题:

***I'm not explaining this well so hopefully this edit makes more sense: basically I have to write code that will work for a ton of test cases, the input below is just an example. So I can't manually enter the input into my function

Say I have the following input:

    0
    4
    0,2,2,3

and I need to generate some sort of output like

    1

How can I do this?

What I mean is, if I'm normally given a problem, I can just define a function and then input the values manually, but how do I read raw data (perform a series of functions/operations on the data)?

(For the assignment I am supposed to receive input on STDIN -> and print correct output on STDOUT

回答1:

STDIN is just a file (or a file-like object) represented by sys.stdin; you can treat it like a normal file and read data from it. You can even iterate over it; for example:

sum = 0
for line in sys.stdin:
    item = int(line.strip())
    sum += item
print sum

or just

entire_raw_data = sys.stdin.read()
lines = entire_raw_data.split()
... # do something with lines

Also, you can either iteratively call raw_input() which returns successive lines sent to STDIN, or even turn it into an iterator:

for line in iter(raw_input, ''):  # will iterate until an empty line
    # so something with line

which is equivalent to:

while True:
    line = raw_input()
    if not line:
        break
    # so something with line

See also: https://en.wikibooks.org/wiki/Python_Programming/Input_and_output



回答2:

We can easily use raw_input() for this case:

text_input = raw_input("Enter text:")

If you're using Python 3, you can use input() in the same way:

text_input = input("Enter text:")

Or, if you prefer to run your program with command line arguments, use sys.argv

import sys
for i in sys.argv:
    if i >= 1:
        command = i
        #do something with your command

Here is a good read: http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch06s03.html

EDIT

Okay, just understand the real question here.

The simple way: you store the data in a text file, and read it with your program.

f = open("path/to/command.txt", 'r')
commmands = f.read()

This way you can process your data quickly. After processed, you can write it down to another file:

output_file = open("path/to/output.txt", 'w')
output_file.write(result)

As what to do with your command file, you can structure it yourself and process it with str.split() method, and loop through it.

Tips: So you don't forget to close the file, it is recommended to use the with statement:

with open('file.txt', 'w') as f:
   #do things
   f.write(result)

More on file processing:

http://docs.python.org/3.3/tutorial/inputoutput.html#reading-and-writing-files

Hope that helps!



回答3:

You can use the function raw_input() to read from the standard input and then process as you need.



回答4:

use this raw_input() it's the basic python input function.

myInput = raw_input()

for more information about raw input please refer to:

http://docs.python.org/2/library/functions.html#raw_input



回答5:

Usually you want to do:

the_input = input(prompt)        # Python 3.x

or

the_input = raw_input(prompt)        # Python 2.x

And then:

print(output)        # Python 3.x

or

print output        # Python 2.x

However, you can also (but probably don't want to) do this:

import sys
the_input = sys.stdin.readline()
bytes_written = sys.stdout.write(output)

This is more or less what print and input do behind the scenes. sys.stdin, sys.stdout (and sys.stderr) work just like files - you can read and write to them etc. In Python terminology they are known as file-like objects.

From what I understand, you want something like this:

def f(x, y, z):
    return 2*x + y + z + 1

a = int(input("Enter a number: "))        # presuming Python 3.x
b = int(input("Enter another number: "))
c = int(input("Enter the final number: "))
print(f(a, b, c))

If run, that would look something like this:

>>> Enter a number: 7
>>> Enter another number: 8
>>> Enter the final number: 9
>>> 32