如何将多行文本粘贴到python输入中 [英] How to paste multiple lines of text into python input

查看:79
本文介绍了如何将多行文本粘贴到python输入中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下输入代码行:

I currently have the following line of code for an input:

rawdata = raw_input('please copy and paste your charge discharge data')

当在 Ipython 中使用 Enthoughts GUI 并运行我的脚本时,我可以复制和粘贴预格式化的文本,这可以很好地提取 \t 和 \n.然而,当尝试将数据粘贴到脚本的终端样式版本中时,它会尝试处理每一行数据,而不是将其作为批量接受.有什么帮助吗?

When using Enthoughts GUI with Ipython and I run my script I can copy and paste preformatted text fine which pulls with it the \t and \n. When trying to paste data into a terminal style version of the script it however tries to process each line of data rather than accepting it as bulk. Any help?

更多相关代码行:

rawed = raw_input('Please append charge data here: ')     
    time, charge = grab_and_sort(rawed)

def grab_and_sort(rawdata):

    rawdata = rawdata.splitlines()
    ex = []
    why = []

    for x in range(2 , len(rawdata)):  
        numbers = rawdata[x].split('\t')
        ex.append(numbers[0])
        why.append(numbers[1])

    ex = array(ex)    
    why = array(why)  

    return (ex, why)

推荐答案

raw_input 接受任何输入直到输入换行符.

执行您要求它接受更多条目的最简单方法,直到遇到文件结尾.

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.

这篇关于如何将多行文本粘贴到python输入中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆