如何将多行文本粘贴到Python输入中[重复]

问题描述 投票:0回答:1

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

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

当使用 Enthought 的 GUIIPython 并运行我的脚本时,我可以复制并粘贴预先格式化的文本,这会拉动和 。然而,当尝试将数据粘贴到脚本的终端样式版本时,它会尝试处理每一行数据,而不是批量接受它。我该如何解决这个问题?

更多相关代码行:

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)
python textinput
1个回答
5
投票

raw_input
接受任何输入直到输入新行字符

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

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)

然后对整批文本执行某些操作。

© www.soinside.com 2019 - 2024. All rights reserved.