如何从文本文件中打印随机完整 link?
How do I print a random full link from a text file?
在文本文件“unt.txt”中,有 38 个 link 各占一行。如何从文件中随机打印 link?下面的代码只是 returns link 中随机数量的字符,而不是整个 link.
from os import close, read
import random
rnd_num = random.choice(range(1,39))
File = open("unt.txt", "r")
print (File.readline(rnd_num))
File.close()
这应该有效。
with open("unt.txt","r") as f:
urls = f.readlines()
import random
print(random.choice(urls))
这有效:
lines = open('text.txt').read().splitlines()
line = random.choice(lines)
print(line)
在文本文件“unt.txt”中,有 38 个 link 各占一行。如何从文件中随机打印 link?下面的代码只是 returns link 中随机数量的字符,而不是整个 link.
from os import close, read
import random
rnd_num = random.choice(range(1,39))
File = open("unt.txt", "r")
print (File.readline(rnd_num))
File.close()
这应该有效。
with open("unt.txt","r") as f:
urls = f.readlines()
import random
print(random.choice(urls))
这有效:
lines = open('text.txt').read().splitlines()
line = random.choice(lines)
print(line)