PYTHON:我想对我所有的txt文件数据进行分类,但每次我尝试分类时都会说我缺少所需的数据

2024-05-16 12:52:49 发布

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

这是我现在设计的代码

import sys                 #so that later on I can exit easily

CoinCountData = open("CoinCountData.txt","r")
class Volunteer:
 def __init__(self,name,coin_type,weight_of_bag,TrueCount):
    self.name = (name)
    self.coin_type = (coin_type)                            #a function allowing me to class the data
    self.weight_of_bag = (weight_of_bag)
    self.TrueCount = (TrueCount)

我还有一个小的数据测试部分,它试图将我的txt文件的第二行变成一个类

volunteer1 = Volunteer(CoinCountData.readlines()[1])

但是,系统会提示我以下消息

Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\CW\!Coincount\coincount.py", line 14, in <module>
    volunteer1 = Volunteer(CoinCountData.readlines()[1])
TypeError: __init__() missing 3 required positional arguments: 'coin_type', 'weight_of_bag', and 'TrueCount'

我不明白如何实际分类这些数据。似乎它认为整件事都是一个名字,我通过改变实验的代码发现了问题所在:

import sys                 #so that later on I can exit easily

CoinCountData = open("CoinCountData.txt","r") 

class Volunteer:

    def __init__(self,name):
        self.name = (name)


volunteer1 = Volunteer(CoinCountData.readlines()[1])

print(volunteer1.name)

当我运行程序时,它打印了整行(Malcolm,1p,3356.00,Y

就上下文而言,这是我制作硬币计数器的项目,给我的硬币计数数据如下:

Abena,5p,325.00,Y
Malcolm,1p,3356.00,Y
Jane,£2,120.00,Y
Andy,£1,166.25,N
Sandip,50p,160.00,Y
Liz,20p,250.00,Y
Andy,20p,250.00,Y
Andy,50p,160.00,Y
Jane,£1,183.75,N
Liz,£,179.0,N
Liz,50p,170.0,N
Jane,50p,160.0,Y
Sandip,£1,183.0,N
Jane,£2,132.0,N
Abena,1p,3356.0,N
Andy,2p,250.0,N
Abena,£1,175.0,Y
Malcolm,50p,160.0,Y
Malcolm,£2,175.0,N
Malcolm,£1,175.0,Y
Malcolm,1p,356.0,Y
Liz,20p,250.0,Y
Jane,£2,120.0,Y
Jane,50p,160.0,Y
Andy,£1,175.0,Y
Abena,1p,359.56,N
Andy,5p,328.5,N
Andy,£2,108.0,N
Malcolm,£2,12.0,N

数据是垂直列出的,因此它显示Abena,5p,325.00,Y,然后下面是Malcolm,1p,3356.00,Y等等

我理解文本文件中的逗号不能作为python中实际的合法逗号使用的问题,我也不允许更改实际的文本文件,因此请告诉我如何创建一个完整的类来输入所有变量


Tags: ofnameselftypebagcoinandyweight
3条回答

您得到的错误消息非常简单

Traceback (most recent call last): File "C:\Users\Owner\Desktop\CW!Coincount\coincount.py", line 14, in volunteer1 = Volunteer(CoinCountData.readlines()[1]) TypeError: init() missing 3 required positional arguments: 'coin_type', 'weight_of_bag', and 'TrueCount

它基本上告诉您在实例化Volunteer对象时缺少3个参数:coin_typeweight_of_bagTrueCount(顺便说一句,这些参数应命名为true_count,以便与其余参数一致)

现在,如果您仔细查看您的代码:

class Volunteer:
 def __init__(self,name,coin_type,weight_of_bag,TrueCount):
     ...

在这里可以看到,当实例化Volunteer对象时,需要给它4个参数:namecoin_typeweight_of_bagTrueCountself不算作需要提供的参数)

现在,在实例化对象时,请执行以下操作:

volunteer1 = Volunteer(CoinCountData.readlines()[1])

您只给出一个参数(即name)。您还需要为coin_typeweight_of_bagTrueCount参数提供值

应该是这样的:

volunteer1 = Volunteer(CoinCountData.readlines()[1], "some coin type", 42, 14)

其中name的值为CoinCountData.readlines()[1]coin_type的值为some coin type,依此类推

阅读线

我想你以后还会讨论更多的问题,因为在你的例子中readlines()的用法似乎不正确

readlines()方法返回一个列表,其中包含文件(https://docs.python.org/3/distutils/apiref.html?highlight=readlines#distutils.text_file.TextFile.readlines)中的每一行

所以,如果你想按照你开始的方式去做,你需要在某个地方有一个循环来循环每一行

CSV

您正在使用的数据文件是CSV(逗号分隔值)。这意味着每一行都是一个数据项,每个值用逗号分隔

您应该看看Python文档,了解如何读取CSV文件:https://docs.python.org/3.6/library/csv.html

readlines返回字符串列表,每个字符串表示文件中的一行

您需要将线拆分为它们的组件,可以使用

CoinCountData.readlines()[1].split(',')

然后,您可以使用参数解包来创建志愿者类的实例:

Volunterr(*CoinCountData.readlines()[1].split(','))

请注意星号(*)

您应该迭代打开的文件并拆分每一行以提供所需的参数:

class Volunteer:
 def __init__(self,name,coin_type,weight_of_bag,TrueCount):
    self.name = name
    self.coin_type = coin_type        #a function allowing me to class the data
    self.weight_of_bag = weight_of_bag
    self.TrueCount = TrueCount

with open("CoinCountData.txt","r") as CoinCountData:
    volunteers = []
    for line in CoinCountData 
        volunteers.append(Volunteer(*line.strip().split(',')))

相关问题 更多 >