简体   繁体   中英

AttributeError: module 'app.parse' has no attribute 'sheet'

I got an error: AttributeError: module 'app.parse' has no attribute 'sheet4' . In parse.py I wrote

class DataRate():
    data_rate ={}
    data_rate =defaultdict(dict)
    def try_to_int(arg):
        try:
            return int(arg)
        except:
            return arg

    book4 = xlrd.open_workbook('./data/excel1.xlsx')
    sheet4 = book4.sheet_by_index(0)

in data_rate_savedata.py

from . import parse
def data_rate_save():
   for row_index in range(0, sheet4.nrows):
        row = sheet4.row_values(row_index)
        row = list(map(try_to_int, row))
        value = dict(zip(tag_list, row))

        closing_rate_dict[value['ID']].update(value)
        user = User.objects.filter(corporation_id=closing_rate_dict[value['ID']]['NAME'])

in main_save.py

from app.parse import DataRate

#parse
DataRate()
#save
data_rate_save()

When I run main_save.py, the error occurrs. I really cannot understand why this error happens because I import parse.py in data_rate_savedata.py, so I can access parse.py of 'sheet4' in data_rate_savedata.py. Should I write something in main_save.py? How can I fix this error? in models.py

class User(models.Model):
    trunsaction_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    regist_date = models.DateTimeField(auto_now=True)
    user_id = models.CharField(max_length=200,null=True)
    name = models.CharField(max_length=200,null=True)
    age = models.CharField(max_length=200,null=True)

Full traceback is

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/app/data_rate_savedata.py", line 11, in <module>
    data_rate_save()
  File "/Users/app/data_rate_savedata.py", line 19, in data_rate_save
    for row_index in range(0, sheet4.nrows):
AttributeError: module 'app.parse' has no attribute 'sheet4'

in your data_rate_savedata.py , try:

from .parse import DataRate

# and use
# DataRate.sheet4

I guess sheet4 is a attribute of DataRate, not parse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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