簡體   English   中英

如何在 Python 中讀取多行輸入?

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

Python 3.6.8 - 空閑

我正在嘗試允許使用鏈接進行復制和粘貼輸入。 作為參考,如果有幫助,我正在使用 Spotify 播放列表鏈接。

我想要做的是:將鏈接復制並粘貼到這個小程序中,然后對所有鏈接進行隨機播放。 有沒有辦法在輸入中讀取\\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()

以下將接受輸入,將每一行附加到 inputLinks 數組,直到出現空行。 要輸入,只需粘貼所有鏈接,每行一個,最后一行為空。

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