• Python记录本


    学习Python的路上

    Python初学者,在这里记录一些相关的基本知识,方便之后学习。

    2022.04.12

    1.在指定位置创建指定名称的文件夹

    # 批量 创建文件夹
    import os  # 导入OS模块 
    
    # 注意,直接复制过来的F:\爬虫 路径结尾是没斜杠的。
    #python 中路径结尾必须是 反斜杠,所以直接手动加个反斜杠/就行了。
    path = 'F:\\爬虫/'
    names=["name1","name2","name3","name4"]
    # 创建4个文件夹,名称为name1-4
    for name in names:
        isExists = os.path.exists(path + name)
        # print(path + name)
        # print(isExists)
        if not isExists:
            # os.path.exists(path+str(i)) 创建文件夹 路径+名称
            os.makedirs(path + name)
            # print("%s 目录创建成功" % name)
        else:
            # print("%s 目录已存在" % name)
    
            # 如果文件不存在,则继续上述操作,直到循环结束
            continue
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.文件名称自定义,可以为循环的文件名

       filename_list=["name1","name2","name3","name4"]
       for filename in filename_list:
       	for i in range(0, 7):
            data.to_excel("" + str(filename)+str(i) + ".xlsx")
            print(data)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2022.04.13

    1.根据文件名称整理到相应的文件夹

    current_path = os.getcwd()#获取当前目录
    print('当前目录:'+current_path)
    
    filename_list = os.listdir(current_path)
    print('当前目录下文件:',filename_list)
    
    print('正在分类整理进文件夹ing...')
    for filename in filename_list:
        try:
            name1 ,name2 = filename.split('.')
            if name2 == 'xlsx':
                try:
                    os.mkdir(name1[:-4])
                    print('创建文件夹'+name1[:-4])
                except:
                    pass
                try:
                    shutil.move(current_path+'\\'+filename,current_path+'\\'+name1[:-4])
                    print(filename+'转移成功!')
                except Exception as e:
                    print('移动失败:' + e)
        except:
            pass
    
    print('整理完毕!')
    input()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2.同时循环多个列表

    #错误的写法为:
    for filename in filename_list:    
         for word in words:
             for cookie in cookies:
    #正确的写法为
    for word,cookie,filename in zip(words,cookies,filename_list):
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2022.05.05

    %load_ext autoreload
    %autoreload 2
    
    • 1
    • 2

    %load_ext autoreload:在执行用户代码前,重新装入 软件的扩展和模块。
    autoreload 意思是自动重新装入。
    它后面可带参数。参数意思你要查你自己的版本帮助文件。一般说:

    无参:装入所有模块。
    0:不执行 装入命令。
    1: 只装入所有 %aimport 要装模块
    2:装入所有 %aimport 不包含的模块。

    建议在使用Jupyter前运行

    pip install adtk
    在jupyternotebook中安装包报错如下:
    The following command must be run outside of the IPython shell:
    
        $ pip install adtk
    
    The Python package manager (pip) can only be used from outside of IPython.
    Please reissue the `pip` command in a separate terminal or command prompt.
    
    See the Python documentation for more information on how to install packages:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    #更新pip 在cmd窗口运行pip
    python -m pip install --upgrade pip
    pip install adtk
    
    • 1
    • 2
    • 3
    jupyter notebook 出现 In [*]
    
    • 1

    只是进程卡住了,可以选择耐心等待或在 jupyter notebook 的 状态监控dos窗口里,敲几次回车,直到没有新的行蹦出来就可以恢复正常了

    jupyter notebook 右上角显示not connected
    原因大概率是不小心关闭了命令行窗口
    在这里插入图片描述
    Python3.9 遇到报错
    module ‘typing’ has no attribute ‘_SpecialForm’
    原因Python3.9+typing-extensions4.2.0不合适
    在这里插入图片描述
    解决办法如下:
    降低typing-extensions版本

    #在cmd中改换typing-extensions的版本
    pip install typing-extensions==4.1.1
    
    • 1
    • 2

    读取csv文件,将其整个转化为列表list

    demand = []
    for line in open('locationdata.csv',encoding='utf-8' ) : #批量读取文件的第一列,[0]列是行名
        demand.append(line.strip())#一次读一行,并且内存不会溢出,去除空行或者空格
    print(demand)
    
    • 1
    • 2
    • 3
    • 4

    读取csv文件,将其某一列转化为列表list

    data = pd.read_csv('locationdata.csv')
    demand = []
    for line in data["A"] : #读取文件的某一列,A是对应的列名
        demand.append(line)
    print(demand)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    antv G6在vue项目中的实践总结
    【杂记-浅谈OSPF协议中的RouterDeadInterval】
    中高级Java程序员,你不得不掌握的基本功,挑战20k+
    [附源码]计算机毕业设计JAVA化妆品销售管理系统
    【CPP_Primer_Plus】C++ IDE推荐
    Burp+Xray的联动使用
    数据结构题型14-二叉树的遍历
    internship:编写excel表的上传方法(导入)
    SpringBoot实现SSE构建实时数据单向推送
    TSINGSEE青犀智慧广场智能监控解决方案,助力广场监控数字化转型
  • 原文地址:https://blog.csdn.net/m0_54401583/article/details/124146894