Python Learning Note 2-parsing data

Source: Internet
Author: User

Import OS; --Python comes with

Print (OS.GETCWD ())--Get the current working directory

Os.chdir ('/users/longlong/documents ')--Convert to/users/longlong/documents directory

Os.path.join (Parm1, Parm2,...)-Constructs a path name from one or more path fragments.

Os.path.expanduser ()--used to extend the path containing the ~ symbol to the full path

>>> pathname = ‘/Users/pilgrim/diveintopython3/examples/humansize.py‘
>>> os.path.split(pathname)--(‘/Users/pilgrim/diveintopython3/examples‘, ‘humansize.py‘)罗列目录内容constructing an absolute path

>>> Import os>>> Print (OS.GETCWD ())/users/longlong/documents>>> Os.chdir ("./python/") >>> os.getcwd () '/users/longlong/documents/python ' >>> print (Os.path.realpath (' whileloop.py ')      

List parsing

>>> [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 ']

Dictionary parsing

>>> 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_size871

>>> 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 '}

Set resolution

>>> A_set = set (range) >>> a_set{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}>>> {x**2 for x in a_set}{0, 1 , 4, 0, 9, +, $, Bayi, 25}>>> {x for x in A_set if x%2 = =}{0, 8, 2, 4, 6}>>> {2**x for x in rang E (10)}{32, 1, 2, 64, 4, 128, 256, 512, 8, 16}

Python Learning Note 2-parsing data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.