如何在列表(Python3.3)中选择列(即:0、1、2、3)

2024-04-25 05:17:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我现在的代码:

def ImportFile(FileName):
    templist =[]
    fileHandle = open('keywords.CSV','r') # Open the file in read only mode
    # For each line in the file 
    for line in fileHandle: 
        File = line.strip() # Cleans the line of text and gets rid of newline characters etc
        fileHandle_list.append(temp.split(',')) # Converts the line of text into a list of items     which were seperated by ","
    return fileHandle_list

''' randomise list '''
def RandomiseList():
    # Go through picking an item at random until I have the whole list
    fileHandle_list = []
    for item in random.sample(keywords, len(keywords)):
        fileHandle_list.append(item)
        print(fileHandle_list)
    return fileHandle_list

这是它将被放置的地方。你知道吗

我试过很多东西,但都不能用,谢谢你的帮助。你知道吗


Tags: ofthetextinforreturndefline
1条回答
网友
1楼 · 发布于 2024-04-25 05:17:04

如果您只是尝试随机排列csv文件中的行顺序,则类似于:

import csv
import random

with open('keywords.CSV', 'rb') as fin:
    csvin = csv.reader(fin)
    file_handle_list = sorted(csvin, key=lambda L: random.random())

或者将其分离以加载列表,然后稍后再“洗牌”:

file_handle_list = list(csvin)
# other stuff
random.shuffle(file_handle_list)

相关问题 更多 >