热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用python从.txt文件中提取随机单词?

我有一个.txt文件,如下所示:series:[{name:"Document

我有一个.txt文件,如下所示:

series: [
{
name: "Document",data: [
{
x: "https://www.frankspin.nl/",y: [0.0,Math.floor(470.8370000589639)],},{
x: "https://www.frankspin.nl/images/frankspin-logo-text.svg",y: [0,0],{
x: "https://www.frankspin.nl/dist/app.bundle.js?1546009674",}
]
},{
name: "Image",y: [Math.floor(485.8770000282675),Math.floor(895.964999916032)],{
name: "Script",y: [Math.floor(897.501999977976),Math.floor(1264.1739998944104)],}
]
}
],

我想从此文件中提取100个随机标题,如下所示:

Title | Author
-------------------------
title1 | author1
title2 | author2
... ...
titleN | authorN

我尝试过:

title1
title2
...
title100

但是在执行期间,程序还会打印随机的作者姓名。如何避免这种情况?


执行此操作时:

with open(path,'r') as f:
title = f.read().split('|')

f.read()为您提供整个文件的字符串。将|上的内容拆分成一个既包含作者又包含标题(以及新行和空格)的列表。

相反,您可以处理行并随时进行拆分。像这样:

with open(path) as f:
titles = [l.split('|')[0].strip() for l in f]

这将为您提供干净的标题列表,例如:

['title1','title2','title3','title4','title5']

因此,您可以使用random.sample()来获取想要的许多随机项目。

import random
path = "path/to/file.txt"
n = 100
with open(path) as f:
titles = [l.split('|')[0].strip() for l in f]
random.sample(titles,n)

这是假设您不想重复。

,

您可以使用struct ContentView: View {
@State var sliderValue: Double = 0.5
var body: some View {
VStack {
Text("SliderValue: \(sliderValue)")
// Slider(value: $sliderValue).accentColor(.red).padding(.horizontal)
SwiftUISlider(
thumbColor: .green,minTrackColor: .red,maxTrackColor: .blue,value: $sliderValue
).padding(.horizontal)
}
}
}
代替.readlines()逐行读取文件到列表。然后,在选择随机行之后,可以使用.read()仅显示其中的标题部分:

.split('|')[0].strip()

或者,您可以在读取文件后立即对其进行处理:

import random
with open('file.txt','r') as f:
title = f.readlines()
for i in range(0,100):
choice = random.choice(title)
print(choice.split('|')[0].strip())

这是import random
with open('file.txt','r') as f:
title = [line.split('|')[0].strip() for line in f.readlines()]
for i in range(0,100):
print(random.choice(title))
的工作方式演示:

.split('|')[0].strip()
,

读完title后,看看。如果我的文本文件是

title1 | author1
title2 | author2

title将是['title1 ',' author1\ntitle2 ',' author2\n']。从此列表中随机选择有时会给您标题,有时是作者,有时两者兼有。

更好的方法如下:

import random
# read in the file and split lines
with open("file.txt","r") as f:
lines = f.read().splitlines()
# lines = ["title1 | author1","title2 | author2"]
titles = [line.split("|")[0].strip() for line in lines]
# titles = ["title1","title2"]

请注意,我们需要调用strip,以去除标题末尾的多余空格。

您现在可以进行采样了,但是我怀疑您想要100个唯一标题,而不仅仅是100个随机标题。您正在做的事情称为sampling with replacement,而获得唯一标题将是sampling without replacement。您可以使用random.sample来完成此操作,如下所示(请参见the random docs):

print(*(random.sample(titles,100)),sep = "\n")

或等效地使用更熟悉的语法

for samp_title in random.sample(titles,100):
print(samp_title)

推荐阅读
author-avatar
煙feldker_231
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有