【问题标题】:How to process raw data (in python)? [closed]如何处理原始数据(在python中)? [关闭]
【发布时间】:2013-10-12 16:51:24
【问题描述】:

***我没有很好地解释这一点,所以希望这个编辑更有意义:基本上我必须编写适用于大量测试用例的代码,下面的输入只是一个示例。所以我不能手动将输入输入到我的函数中

假设我有以下输入:

    0
    4
    0,2,2,3

我需要生成某种输出,例如

    1

我该怎么做?

我的意思是,如果我通常遇到问题,我可以定义一个函数,然后手动输入值,但是我如何读取原始数据(对数据执行一系列函数/操作)?

(对于作业,我应该在 STDIN 上接收输入 -> 并在 STDOUT 上打印正确的输出

【问题讨论】:

  • print 1 '-) 你什么也没说输入与预期输出的关系。
  • Python 3:print(1)。问题解决了!

标签: python input stdout output stdin


【解决方案1】:

STDIN 只是一个由sys.stdin 表示的文件(或类似文件的对象);您可以将其视为普通文件并从中读取数据。你甚至可以迭代它;例如:

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

或者只是

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

此外,您可以迭代调用raw_input(),它会返回发送到 STDIN 的连续行,甚至可以将其转换为迭代器:

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

相当于:

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

另请参阅:https://en.wikibooks.org/wiki/Python_Programming/Input_and_output

【讨论】:

  • 是的,我的问题措辞很糟糕,但这主要是我要问的,我怎样才能“读取”python 中的数据并且 sys.stdin 完成这项工作
【解决方案2】:

对于这种情况,我们可以很容易地使用raw_input()

text_input = raw_input("Enter text:")

如果您使用的是 Python 3,则可以以同样的方式使用 input()

text_input = input("Enter text:")

或者,如果您更喜欢使用命令行参数运行程序,请使用sys.argv

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

这是一个很好的阅读:http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch06s03.html

编辑

好的,这里就明白真正的问题了。

简单的方法:将数据存储在文本文件中,然后用程序读取。

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

这样您可以快速处理您的数据。处理后,可以写到另一个文件中:

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

至于如何处理你的命令文件,你可以自己构造它并用str.split()方法处理它,然后循环遍历它。

提示:所以不要忘记关闭文件,建议使用with语句:

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

关于文件处理的更多信息:

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

希望有帮助!

【讨论】:

    【解决方案3】:

    您可以使用函数raw_input()从标准输入中读取,然后根据需要进行处理。

    【讨论】:

      【解决方案4】:

      使用这个 raw_input() 它是基本的 python 输入函数。

      myInput = raw_input()
      

      有关原始输入的更多信息,请参阅:

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

      【讨论】:

        【解决方案5】:

        通常你想这样做:

        the_input = input(prompt)        # Python 3.x
        

        the_input = raw_input(prompt)        # Python 2.x
        

        然后:

        print(output)        # Python 3.x
        

        print output        # Python 2.x
        

        但是,您也可以(但可能不想)这样做:

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

        这或多或少是printinput 在幕后所做的。 sys.stdinsys.stdout(和sys.stderr)就像文件一样工作——你可以读写它们等等。在 Python 术语中,它们被称为类文件对象。

        据我了解,您想要这样的东西:

        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))
        

        如果运行,它看起来像这样:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-27
          • 1970-01-01
          • 2016-10-01
          • 2018-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多