简体   繁体   English

如何在 Python 中读取多行输入?

[英]How can I read multiple line inputs in Python?

Python 3.6.8 - IDLE Python 3.6.8 - 空闲

I am trying to allow for a copy and paste input with links.我正在尝试允许使用链接进行复制和粘贴输入。 For reference if it helps, I am using Spotify playlist links.作为参考,如果有帮助,我正在使用 Spotify 播放列表链接。

What I'm trying to do: Copy and paste links into this little program and shuffle all of the links.我想要做的是:将链接复制并粘贴到这个小程序中,然后对所有链接进行随机播放。 Is there a way to read \\n in the input, or maybe just check for spaces/multiple-inputs?有没有办法在输入中读取\\n ,或者只是检查空格/多输入?

import random

inputlinks = input("Paste your links: ")
links = inputlinks.replace(" ", "").replace("https://"," httpshhttps://").split("httpsh")
blanklist = []

def randomizer():
    global blanklist
    while len(links) > 0:
        indexed = links[random.randint(0,len(links)-1)]
        blanklist.append(indexed)
        links.remove(indexed)
    blanklist = str(blanklist)
    blanklist = blanklist.replace("'", "").replace(" , ", " ").replace("[","").replace("]","")
    with open("shuffled.txt", "w") as saving:
        saving.write(blanklist)

randomizer()

The following will take input, appending each line to the inputLinks array, until an empty line.以下将接受输入,将每一行附加到 inputLinks 数组,直到出现空行。 To input, simply paste all links, one on each line, with the last line being empty.要输入,只需粘贴所有链接,每行一个,最后一行为空。

inputLinks = []
nextLink = input()
while len(nextLink) > 0:
    inputLinks.append(nextLink)
    nextLink = input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM