Python类字符串类型参数显示为浮点类型

时间:2018-10-30 13:30:05

标签: python

如果有人能帮助我弄清楚我做错了什么,我将不胜感激。我的SupportDocument类中的参数之一称为Preferred_name。它代表文件的首选名称。我有一种确定使用原始名称还是Preferred_name的方法。运行程序时,出现以下错误:

TypeError: object of type 'float' has no len()

...在代码的这一步:

    elif len(self.preferred_name) > 4:
        target_name = self.preferred_name

问题是preferred_name的值是字符串。这些值是从Excel工作表中检索的,并且我已经验证了没有浮点数。

通过将以上内容更改为以下内容,我可以解决该错误:

        elif len(str(self.preferred_name)) > 4:
            target_name = self.preferred_name

但是,我仍在尝试找出在初始化类时做错了什么,以便preferred_name可以解释为浮点类型。

相关代码如下。

谢谢。

SupportDocument:

class SupportDocument(object):

    source_link: object

    def __init__(self, source_link, original_name, target_dir, preferred_name=''):
        self.source_link = source_link
        self.original_name = original_name
        self.target_dir = target_dir
        self.preferred_name = preferred_name

    def get_full_url(self):
        full_url = home_page + self.source_link
        return full_url

    def get_target_full_dir(self):
        if self.target_dir is None:
            target_full_dir = settings.DEFAULT_DOC_DIR
        elif len(self.target_dir) > 3:
            target_full_dir = os.path.join(settings.TARGET_ROOT_DIR, self.target_dir)
        else:
            target_full_dir = settings.DEFAULT_DOC_DIR
        return target_full_dir

    def get_target_name(self):
        if self.preferred_name is None:
            target_name = self.original_name
        elif len(str(self.preferred_name)) > 4:
            target_name = self.preferred_name
        else:
            target_name = self.original_name
        return target_name

    def download(self, session):
        target_path = self.get_target_full_dir()
        os.chdir(target_path)
        schema_get = session.get(self.get_full_url(), verify=False)
        file_name = self.get_target_name()
        with open(os.path.join(self.target_dir, file_name), "wb") as code:
            code.write(schema_get.content)

这是我的main():

def main():

    doc_count = 0
    with requests.Session() as s:
        authenticate(s)
        df = pd.read_excel(io=settings.DATA_FILE, sheet_name='XpressfeedDocs', usecols='A:D')
        logger.info("Opening data file {}.".format(settings.DATA_FILE))
        dfx = df.head(5)
        for i in df.index:
            source_link = df.iloc[i, 0]
            original_name = df.iloc[i, 1]
            file_name = df.iloc[i, 2]
            target_dir = df.iloc[i, 3]
            doc = SupportDocument(source_link, original_name, target_dir, file_name)
            logger.debug("Starting download of file {}...".format(original_name))
            doc.download(s)
            doc_count += 1
main()

跟踪:

Traceback (most recent call last):
  File "C:/Users/xxxxx/sample_code.py", line 4, in <module>
    from xf_docs import SupportDocument
  File "C:\Users\xxxxx\xf_docs.py", line 214, in <module>
    main()
  File "C:\Users\xxxxx\xf_docs.py", line 201, in main
    doc.download(s)
  File "C:\Users\xxxxx\xf_docs.py", line 149, in download
    file_name = self.get_target_name()
  File "C:\Users\xxxxx\xf_docs.py", line 136, in get_target_name
    elif len(self.preferred_name) > 4:
TypeError: object of type 'float' has no len()

0 个答案:

没有答案