Python:提取特定的列数据并将其存储到变量中

时间:2018-11-05 22:23:00

标签: python csv class object variables

我有一个csv文件,我想从中提取评级和评论字段,并将其存储在两个变量-评级和评论中。此过程完成后,我需要查看提取的数据。 CSV文件中存储的数据如下:

enter image description here

在我的dataclean python文件中,到目前为止编写的代码是:

class Extractdata:

    def __init__(self, rating, comment):
        self.rating = rating
        self.comment = comment
requirement_list = []
import csv
with open('D://Python//testml//my-tracks-reviews.csv', encoding='utf-8') as fileread:
    filereader = csv.DictReader(fileread, delimiter=';', quotechar='"')
    next(filereader, None)  # Skip the header.
    # Unpack the row directly in the head of the for loop.
    for rating, comment_text in filereader:
        # Get the data in the variable instances.
        rating = int(rating)
        comment = comment_text
        # Now create the requirement instance and append it to the list.
        requirement_list.append(Extractdata(rating, comment))

# View the data

我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/Sam/PycharmProjects/ReqPrio/preprocess.py", line 12, in <module>
    for rating, comment_text in filereader:
ValueError: not enough values to unpack (expected 2, got 1)

Process finished with exit code 1

还有谁能建议如何从另一个文件(称为main.py)访问此文件中的rating变量,以计算评分的平均值?

1 个答案:

答案 0 :(得分:1)

csv.DictReader返回一个迭代器,该迭代器生成作为dict的行,因此您应该使用其键访问每一行的列:

for row in filereader:
    rating = int(row['rating'])
    comment = row['comment_text']
    requirement_list.append(Extractdata(rating, comment))

您还应该删除跳过标头的行,因为csv.DictReader已经读取了第一行作为标头。