原代码复制重写,报以下错误,我也很奇怪这个dict到底是谁,怎么来的,传什么参?

   if dict._contais_('topics') == False:
AttributeError: 'dict' object has no attribute '_contais_'

dict.__contains__()

两边是各有两个下划线

if dict._contais_('topics') = False:错了,应该是if dict.__contains__('topics') == False:,少了个n,而且要写成双下划线,dict有__contains__属性。

d={'x':'abcde'}
print(d.__contains__('x'))
#True

 

dict.__contains__()

 

对于后一个问题可以将save函数的第二个参数改成单个文件传递,通过循环调用写入文件。

 

excelname是列表,需要用“/”拼接成字符串

如果save()函数中file_name_or_filelike_obj需要保持是列表

就另外生成一个字符串传给open

file_path_str = "/".join(file_name_or_filelike_obj)

f = open(file_path_str, 'w+b')

 

import xlwt
# 传入xmind的路径,返回dict的列表
from xmindparser import xmind_to_dict

def resolvePath(dict, lists, title):
    #使用strip去除两边空格
    title = title.strip()

    if len(title) == 0:
        concattitle = dict['title'].strip()
    else:
        concattitle = title + '\t' + dict['title'].strip()
    if dict._contais_('topics') == False:
        lists.append(concattitle)
    else:
        for d in dict['topics']:
            resolvePath(d, lists, concattitle)

def xmind_cat(list, excelname):
    print(f'list is {list}')
    wb = xlwt.Workbook()
    sheet = wb.add_sheet('转化测试', cell_overwrite_ok=True)

    row0 = ['第一行', '第二行', '第三行', '第四行','第五行', '第六行', '第七行']

    for i in range(0, len(row0)):
        sheet.write(0, i , row0[i])

    index = 0
    for x in range(0, len(list)):
        lists = []
        resolvePath(list[x], lists, '')
        print('idid', len(lists))
        print(lists)

        for j in range(0, len(lists)):
            lists[j] = lists[j].split('\t')
            print('listsde jjj', lists[j])

            for n in range(0, len(lists[j])):
                print(lists[j][n])

                sheet.write(j+index+1, 0, j+index+1)
                sheet.write(j+index+1, n+1, lists[j][n])

                if n>= 2:
                    sheet.write(0, n+1, '第几行:'+str(n-1))

        if j == len(lists)- 1 :
            index += len(lists)
    wb.save(excelname)


def maintest(filename):

    otput = xmind_to_dict(filename)

    excelname = filename.split('/')
    xmind_cat(otput[0]['topic']['topics'], excelname)



if __name__ == '__main__':
    fname = 'D:/file_xmind/filexmind/APP与Web测试点.xmind'
    maintest(fname)

 

我意识到了,,确实是少加了,,,

 

 f = open(file_name_or_filelike_obj, 'w+b')
TypeError: expected str, bytes or os.PathLike object, not list

    def save(self, file_name_or_filelike_obj, stream):
        # 1. Align stream on 0x1000 boundary (and therefore on sector boundary)
        padding = b'\x00' * (0x1000 - (len(stream) % 0x1000))
        self.book_stream_len = len(stream) + len(padding)

        self._build_directory()
        self._build_sat()
        self._build_header()
        
        f = file_name_or_filelike_obj
        we_own_it = not hasattr(f, 'write')
        if we_own_it:
            f = open(file_name_or_filelike_obj, 'w+b')
        f.write(self.header)
        f.write(self.packed_MSAT_1st)
        # There are reports of large writes failing when writing to "network shares" on Windows.
        # MS says in KB899149 that it happens at 32KB less than 64MB.
        # This is said to be alleviated by using "w+b" mode instead of "wb".
        # One xlwt user has reported anomalous results at much smaller sizes,
        # The fallback is to write the stream in 4 MB chunks.
        try:
            f.write(stream)
        except IOError as e:
            if e.errno != 22: # "Invalid argument" i.e. 'stream' is too big
                raise # some other problem
            chunk_size = 4 * 1024 * 1024
            for offset in xrange(0, len(stream), chunk_size):
                f.write(buffer(stream, offset, chunk_size))
        f.write(padding)
        f.write(self.packed_MSAT_2nd)
        f.write(self.packed_SAT)
        f.write(self.dir_stream)
        if we_own_it:
            f.close()

 

我重新发了一段报错的save的库的配置的代码

 if j == len(lists)- 1 :
            index += len(lists)
            print('ppppp')
    print(excelname)
    wb.save(excelname)


#打印出来:

ppppp
['D:', 'file_xmind', 'filexmind', 'APP与Web测试点.xmind']


#报错信息
f = open(file_name_or_filelike_obj, 'w+b')
TypeError: expected str, bytes or os.PathLike object, not list

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y