鱼C论坛

 找回密码
 立即注册
查看: 13775|回复: 6

[已解决]python如何输入多行数据

[复制链接]
发表于 2017-3-26 12:24:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
比如想输入:
2
4 5 6
7 8 2
7 5 9
每一行存放在不同的列表里,但是用input().split()只能输入一行

在网上看到这样的写法:
import sys
for line in sys.stdin:
        for value in line.split()
                print value

不是很明白这个的实现,以及我用这段代码的时候,输入一行后就会打印出这一行数据,不能满足我想一次输入多行的要求,是我的输入不对吗?还有其他输入多行数据的办法吗?
最佳答案
2017-3-27 10:14:59
  1. lst = []
  2. print('请输入数据,直接回车结束:')
  3. while True:
  4.     temp = input()
  5.     if temp == '':
  6.         break
  7.     lst.append([int(i) for i in temp.split(' ')])
  8. for i in lst:
  9.     print(i)
复制代码

  1. 请输入数据,直接回车结束:
  2. 2
  3. 4 5 6
  4. 7 8 2
  5. 7 5 9

  6. [2]
  7. [4, 5, 6]
  8. [7, 8, 2]
  9. [7, 5, 9]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-26 13:16:53 | 显示全部楼层
  1. d = []
  2. e = []
  3. f = []
  4. a = int(input('请输入数字:'))
  5. b = int(input('请输入数字:'))
  6. c = int(input('请输入数字:'))
  7. d.append(a)
  8. e.append(b)
  9. f.append(c)
  10. print(d)
  11. print(e)
  12. print(f)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 14:01:30 | 显示全部楼层
  1. import sys
  2. inpls=[]
  3. for line in sys.stdin:
  4.     if line=='\n': break
  5.     inpls.append(line.split())

  6. print (inpls)
复制代码
  1. 2
  2. 4 5 6
  3. 7 8 2
  4. 7 5 9

  5. [['2'], ['4', '5', '6'], ['7', '8', '2'], ['7', '5', '9']]
  6. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 14:06:41 | 显示全部楼层
摘自http://5ydycm.blog.51cto.com/115934/304324
stdin , stdout , 以及stderr 变量包含与标准I/O 流对应的流对象. 如果需要更好地控制输出,而print 不能满足你的要求, 它们就是你所需要的. 你也可以替换它们, 这时候你就可以重定向输出和输入到其它设备( device ), 或者以非标准的方式处理它们
从网上摘抄的文章,供大家参考:
#testing stdout

print 'Hello World!'
运行hello.py就会在标准输出的屏幕上打印 Hello World!, 我们再编一个简单的标准输入的小程序 sayhi.py:
#testing stdin

print 'Hi, %s!' % raw_input('Please enter your name:')
当你用键盘输入你的名字后,程序在屏幕上输出Hi,[你的名字]!, 这就是从标准输入:键盘获取信息,再输出到标准输出:屏幕的例子。
那么上面的例子中print 和 raw_input是如何与标准输入/输出流建立关系的呢?
其实Python程序的标准输入/输出/出错流定义在sys模块中,分别 为: sys.stdin, sys.stdout, sys.stderr
上面的程序分别与下列的程序是一样的:
import sys

sys.stdout.write('Hello World!')
import sys

print 'Please enter your name:',
name=sys.stdin.readline()[:-1]
print 'Hi, %s!' % name

那么sys.stdin, sys.stdout, stderr到底是什么呢?我们在Python运行环境中输入以下代码:
import sys
for f in (sys.stdin, sys.stdout, sys.stderr): print f
输出为:
<open file '<stdin>', mode 'r' at 892210>
<open file '<stdout>', mode 'w' at 892270>
<open file '<stderr>', mode 'w at 8922d0>

由此可以看出stdin, stdout, stderr在Python中无非都是文件属性的对象,他们在Python启动时自动与Shell 环境中的标准输入,输出,出错关联。
而Python程序的在Shell中的I/O重定向与本文开始时举的DOS命令的重定向完全相同,其实这种重定向是由Shell来提供的,与Python 本身并无关系。那么我们是否可以在Python程序内部将stdin,stdout,stderr读写操作重定向到一个内部对象呢?答案是肯定的。
Python提供了一个StringIO模块来完成这个设想,比如:
from StringIO import StringIO
import sys
buff =StringIO()

temp = sys.stdout                               #保存标准I/O流
sys.stdout = buff                                 #将标准I/O流重定向到buff对象
print 42, 'hello', 0.001

sys.stdout =temp                                 #恢复标准I/O流
print buff.getvalue()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 14:33:34 | 显示全部楼层

你的代码每个列表只能输入一个数字,而且限定了列表数。和需求有差距吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 15:56:49 | 显示全部楼层
网妖 发表于 2017-3-26 14:33
你的代码每个列表只能输入一个数字,而且限定了列表数。和需求有差距吧


是啊
你还是请教大佬吧
召唤术!
@冬雪雪冬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-27 10:14:59 | 显示全部楼层    本楼为最佳答案   
  1. lst = []
  2. print('请输入数据,直接回车结束:')
  3. while True:
  4.     temp = input()
  5.     if temp == '':
  6.         break
  7.     lst.append([int(i) for i in temp.split(' ')])
  8. for i in lst:
  9.     print(i)
复制代码

  1. 请输入数据,直接回车结束:
  2. 2
  3. 4 5 6
  4. 7 8 2
  5. 7 5 9

  6. [2]
  7. [4, 5, 6]
  8. [7, 8, 2]
  9. [7, 5, 9]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-3 22:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表