python使用心得

本文介绍了如何使用Python的numpy库将数组数据写入txt文件,以及如何保存和读取list数据。此外,还展示了如何表示大数(如100e+5),并解释了在Python中使用三元操作符的类似方法。同时,讨论了在Python目录结构中,当需要从文件夹导入模块时,添加__init__.py文件的重要性。
摘要由CSDN通过智能技术生成

1 numpy写入数据:

将numpy中的数组写入txt中:

numpy.savetxt("result.txt", numpy_data);

保存list数据

file=open('data.txt','w')
file.write(str(list_data));
file.close()

2 表示指数:

fs = 100e+5

3 类似三元操作符

4 获取执行文件的python文件的绝对目录

参考:

python3中 os.path.realpath(__file__) 的使用_白清羽的博客-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/gufenchen/article/details/90552370

 import os
cwd = os.path.dirname(os.path.realpath('__file__'))
cwd

输出

'C:\\Users\\ZJ\\aaa'

需要注意__file__可能需要加入‘’() 

3 文件导入

python目录中的__init__.py文件详解_python在目录下创建init-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/Victor2code/article/details/106361045

 一个文件夹下有很多文件,则需要在这个文件夹下添加一个__init__.py文件,这主要是因为python不支持from folder import *

python的import问题:绝对路径,相对路径,sys.path.append_sys.path.append 相对路径-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/weixin_44546620/article/details/104755730

在python脚本被执行,python导入其他包或模块时,python会根据sys.path列表里的路径寻找这些包或模块。如果没找到的话,程序就会报错,所以可以在代码中加入

print(sys.path)

来判断当前模块的搜索路径,然后进行代码的调试

4  打成wheel包

pip install --upgrade build
python -m build
5

5 日志输出

在python中,为何调用logging模块却不能正常输出日志?_logging.info不出现-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/qq_30614451/article/details/111030276

    root_logger = logging.getLogger()
    for h in root_logger.handlers:
       root_logger.removeHandler(h)

    logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                    level=logging.DEBUG,
                    filename='/home/test.log',
                    filemode='w')

6 import相关

参考深度好文

 python导入机制及importlib模块_python 的模块加载机制和 importlib 模块-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/jeffery0207/article/details/120612313

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youzjuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值