简体   繁体   English

python程序不读取文本文件,也不将单词放入列表中

[英]python program doesn't read text file and doesn't put the words in the list

I want the program to read a text file and to put every word in a list, here's the code that I've written: 我希望程序读取文本文件并将每个单词放入列表中,这是我编写的代码:

class Teater(object):
    def__init__(self, namn, AntalPlatser, VuxenBiljettpris, 
            PensionärBiljettpris, BarnBiljettpris, 
            vuxen=0, pansionär=0, barn=0):

        self.namn=namn
        self.AntalPlatser=AntalPlatser
        self.VuxenBiljettpris=VuxenBiljettpris
        self.PensionärBiljettpris=PensionärBiljettpris
        self.BarnBiljettpris=BarnBiljettpris
        self.vuxen=vuxen
        self.barn=barn
        self.pansionär=pansionär


def teaterLista():
    infil = open("teatrar.txt", "r", encoding="utf8")
    lista=[]
    lista = lista[4:]
    for rad in lista:
        splitList=rad.split("/")
        namn=splitlist[0]
        AntalPlatser=splitlist[1]
        VuxenBiljettpris=splitlist[2]
        PensionärBiljettpris=splitlist[3]
        BarnBiljettpris=splitlist[4]
        nyTeater = Teater(
            namn, 
            AntalPlatser, 
            VuxenBiljettpris, 
            PensionärBiljettpris,
            BarnBiljettpris)
        lista.append(nyTeater)
    return lista

and my text file looks like this: 我的文本文件如下所示:

Såda Teaterbiljetter
TeaternsNamn/Antal platser i salongen/Vuxenbiljettpris/Pensionärbiljettpris/Barnbiljettpris
------------------------------------------------------------------------------------- 

SodraTeatern/414/330/260/200 

Dramaten/770/390/350/100 

ChinaTeatern/1230/395/300/250

I don't want the first 4 rows of the text file to be printed. 我不希望打印文本文件的前4行。 but when I type läsFil() in python Shell i only get this: [ ] 但是当我在python Shell中键入läsFil()时,我只会得到以下信息:[]

There are many problems with your code, but most notably you don't ever read from the file infil . 您的代码有很多问题,但最值得注意的是,您从未从文件infil读取过信息。 Instead you create an empty list and take a slice on that list, so it is still empty: 相反,您创建一个空列表并对该列表进行切片,因此它仍然是空的:

lista=[]
lista = lista[4:]

Anyway, you should have a look at the CSV module, which can handle for you the reading of delimited text files (whether separated by commas or slashes). 无论如何,您应该看看CSV模块,该模块可以为您处理定界文本文件的读取(无论是用逗号还是斜杠分隔)。

This part of the code looks very suspicious: 这部分代码看起来非常可疑:

infil = open("teatrar.txt", "r", encoding="utf8")  # not used after.
lista=[]
lista = lista[4:]  # does nothing since lista is empty
for rad in lista:

You probably mean: 您可能是说:

infil = open("teatrar.txt", "r", encoding="utf8")
lista=[]
for rad in infil:

Then 然后

namn=splitlist[0]
AntalPlatser=splitlist[1]
VuxenBiljettpris=splitlist[2]
PensionärBiljettpris=splitlist[3]
BarnBiljettpris=splitlist[4]

Could be better written as 最好写成

namn, AntalPlatser, VuxenBiljettpris, PensionärBiljettpris,  BarnBiljettpris =  splitlist[:5]

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

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