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']
>>> [f for f in glob.glob("*.py") if os.stat(f).st_size > 700]
['indices_over_two_objects.py', 'while_loops.py']

字典解析

>>> 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学习笔记2-解析数据的更多相关文章

  1. 吴裕雄--天生自然python学习笔记:WEB数据抓取与分析

    Web 数据抓取技术具有非常巨大的应用需求及价值, 用 Python 在网页上收集数据,不仅抓取数据的操作简单, 而且其数据分析功能也十分强大. 通过 Python 的时lib 组件中的 urlpar ...

  2. Head First Python学习笔记4——处理数据

    有这么几组数据需要你处理: James 2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22 Julia 2.59,2.11,2:11,2:23,3-10,2-23 ...

  3. Python学习笔记:外部数据的输入、存储等操作

    查看current工作路径: >>> import os >>> os.getcwd() 'D:\\python' 更改工作路径: >>> os. ...

  4. Python学习笔记_Chapter 4数据保存到文件

    1. What For 将基于内存的数据存储到磁盘上,达到持续存储. 2. HOW 方法一: 将数据写到文件中 常规的处理方式 #file.x被打开的文件,model打开文件的方式 out=open( ...

  5. python学习笔记之基础数据和控制

    注释: 单行注释 # 多行注释'''    ''' 注意:当注释中有汉字时需要在python文件的第一行添加如下内容之一:#coding:gbk或#coding:utf-8或##-*- coding ...

  6. Python学习笔记之将数据写入到文件中

    10-3 访客:编写一个程序,提示用户输入其名字:用户作出响应后,将其名字写入到文件guest.txt 中. 编写Python代码: username = input("Please ent ...

  7. python学习笔记3.2_数据导出

    一.data.to_csv:数据导出 1.to_csv:将数据导出为逗号分隔的文件 2.输出为其他分隔符的文件 写入到控制台,并打印:sys.stdout na_rep:对空值进行标注 二.serie ...

  8. Python学习笔记三:数据特征分析

    完成数据清理后,下面通过图表展开对数据的分析. 1.前期初判(分布分析): 1)判断分组区间: # a.散点图:plt.scatter(data[字段1],data['字段2'], s = data[ ...

  9. python学习笔记3.1_数据读取常用函数参数

    一.read_table/read_csv常用函数参数 1.path:表明文件系统位置的字符串.url或文件型对象 2.sep或delimiter:用于分隔每行字段的字符序列或正则表达式 3.head ...

随机推荐

  1. git取消跟踪文件

    取消跟踪文件: $git rm --cached FILENAME 取消跟踪目录: $git rm --cached FILENAME -r

  2. springMVC学习笔记(二)-----注解和非注解入门小程序

    最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了.终于在上周五上线了,可以轻松几天了.闲话不扯淡了,继续谈谈springMvc的学习. 现在,用到SpringMvc的大部分使用全注解配置 ...

  3. 8.1 EntityTypeConfiguration Class in Code-First【Code First系列】

    在我们学习Fluent API之前,先来看看Fluent API中重要的类--EntityTypeConfiguration吧. EntityTypeConfiguration类是Fluent API ...

  4. 【原创】Java和C#下String类型中的==和equals的原理与区别

    一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...

  5. asp.net 301重定向代码

    /// <summary> ///重定向代码 /// </summary> /// <param name="sender"></para ...

  6. 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command)

    [源码下载] 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command) 作者:webabcd 介绍背水一战 Windows 10 之 MVV ...

  7. Oracle学习总结_day06_视图&序列&索引

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day 06 视图,索引,序列 视图 什么是视图: 视 ...

  8. file_get_contents()/file_put_contents()

    PHP file_get_contents() 函数 定义和用法 file_get_contents() 把整个文件读入一个字符串中. 该函数是用于把文件的内容读入到一个字符串中的首选方法.如果服务器 ...

  9. UDS(ISO14229-2006) 汉译(No.4 术语和缩写)

    A_PCI                    Application layer Protocol Control Information应用层协议控制消息. A_PDU              ...

  10. No.009:Palindrome Number

    问题: Determine whether an integer is a palindrome. Do this without extra space. 官方难度: Easy 翻译: 不使用额外空 ...