Python:从文件中选择随机行,然后删除该行 [英] Python: Choose random line from file, then delete that line

查看:22
本文介绍了Python:从文件中选择随机行,然后删除该行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手(因为我是通过 CodeAcademy 课程学习的)并且可以使用一些帮助来解决这个问题.

I'm new to Python (in that I learned it through a CodeAcademy course) and could use some help with figuring this out.

我有一个文件TestingDeleteLines.txt",大约有 300 行文本.现在,我正在尝试让它从该文件中随机打印 10 行,然后删除这些行.

I have a file, 'TestingDeleteLines.txt', that's about 300 lines of text. Right now, I'm trying to get it to print me 10 random lines from that file, then delete those lines.

所以如果我的文件有 10 行:

So if my file has 10 lines:

Carrot
Banana
Strawberry
Canteloupe
Blueberry
Snacks
Apple
Raspberry
Papaya
Watermelon

我需要它从这些行中随机挑选,告诉我它是随机挑选的蓝莓、胡萝卜、西瓜和香蕉,然后删除这些行.

I need it to randomly pick out from those lines, tell me it's randomly picked blueberry, carrot, watermelon, and banana, and then delete those lines.

问题是,当 Python 读取一个文件时,它会读取该文件,一旦读取到最后,它就不会返回并删除这些行.我目前的想法是我可以将行写入列表,然后重新打开文件,将列表与文本文件匹配,如果找到匹配项,则删除这些行.

The issue is, when Python reads a file, it reads that file and once it gets to the end, it won't go back and delete the lines. My current thinking was that I could write the lines to a list, then reopen the file, match the list to the text file, and if it finds a match, delete the lines.

我目前的问题有两个:

  1. 它正在复制随机元素.如果它选择一条线,我需要它不再选择同一条线.但是,使用 random.sample 似乎不起作用,因为当我稍后使用每一行附加到 URL 时,我需要将这些行分开.
  2. 我不觉得我的逻辑(写入数组->在文本文件中查找匹配项->删除)是最理想的逻辑.有没有更好的写法?

  1. It's duplicating the random elements. If it picks a line, I need it to not pick that same line again. However, using random.sample doesn't seem to work, as I need those lines separated out when I later use each line to append to a URL.
  2. I don't feel like my logic (write to array->find matches in text file->delete) is the most ideal logic. Is there a better way to write this?

import webbrowser
import random

"""url= 'http://www.google.com'
webbrowser.open_new_tab(url+myline)""" Eventually, I need a base URL + my 10 random lines opening in each new tab

def ShowMeTheRandoms():
    x=1
    DeleteList= []
    lines=open('TestingDeleteLines.txt').read().splitlines()
for x in range(0,10):
    myline=random.choice(lines)
    print(myline) """debugging, remove later"""
    DeleteList.append(myline)
    x=x+1
    print DeleteList """debugging, remove later"""
ShowMeTheRandoms()

推荐答案

我有一个文件TestingDeleteLines.txt",大约有 300 行文本.现在,我正在尝试让它从该文件中随机打印 10 行,然后删除这些行.

I have a file, 'TestingDeleteLines.txt', that's about 300 lines of text. Right now, I'm trying to get it to print me 10 random lines from that file, then delete those lines.

#!/usr/bin/env python
import random

k = 10
filename = 'TestingDeleteLines.txt'
with open(filename) as file:
    lines = file.read().splitlines()

if len(lines) > k:
    random_lines = random.sample(lines, k)
    print("
".join(random_lines)) # print random lines

    with open(filename, 'w') as output_file:
        output_file.writelines(line + "
"
                               for line in lines if line not in random_lines)
elif lines: # file is too small
    print("
".join(lines)) # print all lines
    with open(filename, 'wb', 0): # empty the file
        pass

O(n**2) 算法必要时可以改进(对于像输入这样的小文件,您不需要它)

It is O(n**2) algorithm that can be improved if necessary (you don't need it for a tiny file such as your input)

这篇关于Python:从文件中选择随机行,然后删除该行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆