浅谈Python中数据解析

作者:hebedich 时间:2021-07-02 20:47:00 

Import os; -- Python自带
print(os.getcwd()) -- 获得当前工作目录
os.chdir('/Users/longlong/Documents') -- 转换到/Users/longlong/Documents目录
os.path.join(parm1, parm2,...) -- 从一个或多个路径片段中构造一个路径名。
os.path.expanduser() -- 用来将包含~符号的路径扩展为完整的路径


>>> pathname = '/Users/pilgrim/diveintopython3/examples/humansize.py'
>>> os.path.split(pathname)  --('/Users/pilgrim/diveintopython3/examples', 'humansize.py')罗列目录内容构造绝对路径



>>> import os
>>> print(os.getcwd())
/Users/longlong/Documents
>>> os.chdir("./python/")
>>> os.getcwd()
'/Users/longlong/Documents/python'
>>> print(os.path.realpath('whileloop.py')
      )
/Users/longlong/Documents/python/whileloop.py
>>> print(os.path.realpath("whileloop.py"))
/Users/longlong/Documents/python/whileloop.py
>>>


列表解析


>>> [os.path.realpath(f) for f in glob.glob("*.py")]
['/Users/longlong/Documents/python/indices_over_two_objects.py', '/Users/longlong/Documents/python/loops_over_indices.py', '/Users/longlong/Documents/python/while_loops.py']<br>
>>> [f for f in glob.glob("*.py") if os.stat(f).st_size > 700]
['indices_over_two_objects.py', 'while_loops.py']
1

 
 

字典解析


>>> metadat_dict = { f:os.stat(f) for f in glob.glob('*.py')}

>>> type(metadat_dict)
<class 'dict'>

>>> list(metadat_dict.keys())
['indices_over_two_objects.py', 'while_loops.py', 'loops_over_indices.py']

>>> metadat_dict['indices_over_two_objects.py'].st_size
871


>>> list(metadat_dict.keys())
['indices_over_two_objects.py', 'while_loops.py', 'loops_over_indices.py']

>>> a_dict = {'a':1, 'b':2,'c':3}
>>> {value:key for key,value in a_dict.items()}
{1: 'a', 2: 'b', 3: 'c'}

集合解析


>>> a_set = set(range(10))
>>> a_set
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

>>> {x**2 for x in a_set}
{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}

>>> {x for x in a_set if x%2 == 0 }
{0, 8, 2, 4, 6}

>>> {2**x for x in range(10)}
{32, 1, 2, 64, 4, 128, 256, 512, 8, 16}

以上所述就是本文的全部内容了,希望大家能够喜欢。

标签:Python,数据解析
0
投稿

猜你喜欢

  • 巧用weui.topTips验证数据的实例

    2023-08-12 03:00:51
  • 详解Python的多线程定时器threading.Timer

    2023-04-07 03:33:43
  • asp,php,.net使用301重定向方法

    2007-09-26 14:05:00
  • Python机器学习应用之基于天气数据集的XGBoost分类篇解读

    2023-09-12 05:45:07
  • 内容,而不是Chrome

    2008-10-16 13:43:00
  • Python的10道简单测试题(含答案)

    2021-12-28 03:57:24
  • Python Matplotlib通过plt.subplots创建子绘图

    2022-06-03 08:23:16
  • WorkBench管理操作MySQL

    2010-10-14 14:21:00
  • python matplotlib折线图样式实现过程

    2022-10-28 12:18:08
  • python实现公司年会抽奖程序

    2022-12-19 15:35:27
  • python实现两张图片拼接为一张图片并保存

    2023-01-26 17:56:52
  • 关于adfuller函数返回值的参数说明与记录

    2023-03-07 13:33:14
  • 使用批处理脚本自动生成并上传NuGet包(操作方法)

    2021-12-24 18:05:34
  • ansible-playbook实现自动部署KVM及安装python3的详细教程

    2021-09-02 23:13:12
  • .NET反向代理组件YARP介绍

    2023-07-23 02:29:45
  • python实现堆栈与队列的方法

    2023-06-19 02:57:04
  • asp如何定时执行约定的页面?

    2009-11-15 20:17:00
  • oracle的一些tips技巧

    2009-03-02 11:06:00
  • PHPStudy hosts文件可能不存在或被阻止打开及同步hosts失败问题

    2023-06-08 10:29:10
  • CSS框架带来的效率提升

    2007-12-27 20:01:00
  • asp之家 网络编程 m.aspxhome.com