简体   繁体   English

在python中,将文件读入2D列表后,使用函数,我应该怎么做,不要让2D列表为空

[英]In python how am I supposed to, after reading the file into a 2D list, using a function, not let the 2D list empty

I read in a text file, with a format: ola,2 ola,4 ola,6 我读了一个文本文件,格式为:ola,2 ola,4 ola,6

And i read this file in using the reading function that removes all the spare elements in the list: 我使用读取功能读取了该文件,该功能删除了列表中的所有备用元素:

import csv

def read(name,practicef):
    temp=[]
    name=name+".txt"
    practicef = list(csv.reader(open(name,"r")))
    print(practicef)
    print(len(practicef))
    temp=practicef
    practicef=[]
    for item in temp:
        if item!=[]:
            practicef.append(item)
            print(practicef)
    return practicef

And then when I wanted to compare the elemnts in the list to my password, the list was empty, I am just wondering why it did this and how i could avoid it. 然后,当我想将列表中的元素与密码进行比较时,列表为空,我只是想知道为什么这样做以及如何避免这种情况。 The whole code together as it comes in the sequence is: 整个代码按顺序排列在一起是:

import csv

def read(name,practicef):
    temp=[]
    name=name+".txt"
    practicef = list(csv.reader(open(name,"r")))
    print(practicef)
    print(len(practicef))
    temp=practicef
    practicef=[]
    for item in temp:
        if item!=[]:
            practicef.append(item)
            print(practicef)
    return practicef



practicef=()
name="lollington"
read(name,practicef)
use=input("use")
pw=input("pw")
print(len(practicef))

for i in range(0,len(practicef)):
    print(use,pw)
    print(practicef[i][0],practicef[i][1])
    if use== practicef[i][0] and pw == practicef[i][1]:
        print("successful login")

it's a bit hard to follow what you want to do, however you actually don't use the results of your function. 要执行您的操作有些困难,但是实际上您并不使用函数的结果。

does 确实

practicef = read(name,practicef)

get you closer to where you want to be? 让您更靠近想要去的地方?

here is my output after that change (added a bit extra on the prints so it's easier to see where they are from): 这是更改后的输出(在打印件上增加了一些额外的内容,因此更容易查看它们的来源):

in read [['ola', '2 ola', '4 ola', '6']]
len in read 1
[['ola', '2 ola', '4 ola', '6']]

use: ola

pw: 2
outer list len after fcn call: 1
uid ola pw 2
ola 2 ola

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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