简体   繁体   中英

How can I read multiple line inputs in Python?

Python 3.6.8 - IDLE

I am trying to allow for a copy and paste input with links. For reference if it helps, I am using Spotify playlist links.

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?

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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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