diff --git a/README.md b/README.md index 9f116af..e8ebee7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Comprehensive Python Cheatsheet =============================== -[Download text file](https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md), [PDF](https://gto76.github.io/python-cheatsheet/web/python-cheatsheet-d3a72f9.pdf), [Fork me on GitHub](https://github.com/gto76/python-cheatsheet) or [Check out FAQ](https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions). +[Download text file](https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md), [PDF](https://gto76.github.io/python-cheatsheet/web/python-cheatsheet-eea110f.pdf), [Fork me on GitHub](https://github.com/gto76/python-cheatsheet) or [Check out FAQ](https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions). ![Monty Python](web/image_888.jpeg) @@ -12,7 +12,7 @@ Contents **   ** **2. Types:** **          ** **[`Type`](#type)**__,__ **[`String`](#string)**__,__ **[`Regular_Exp`](#regex)**__,__ **[`Format`](#format)**__,__ **[`Numbers`](#numbers)**__,__ **[`Combinatorics`](#combinatorics)**__,__ **[`Datetime`](#datetime)**__.__ **   ** **3. Syntax:** **         ** **[`Args`](#arguments)**__,__ **[`Inline`](#inline)**__,__ **[`Closure`](#closure)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Duck_Types`](#duck-types)**__,__ **[`Enum`](#enum)**__,__ **[`Exceptions`](#exceptions)**__.__ **   ** **4. System:** **        ** **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#path)**__,__ **[`Command_Execution`](#oscommands)**__.__ -**   ** **5. Data:** **             ** **[`CSV`](#csv)**__,__ **[`SQLite`](#sqlite)**__,__ **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__ +**   ** **5. Data:** **             ** **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`CSV`](#csv)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__ **   ** **6. Advanced:** **   ** **[`Threading`](#threading)**__,__ **[`Operator`](#operator)**__,__ **[`Introspection`](#introspection)**__,__ **[`Metaprograming`](#metaprograming)**__,__ **[`Eval`](#eval)**__,__ **[`Coroutine`](#coroutine)**__.__ **   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profiling)**__,__ **                                 ** **[`NumPy`](#numpy)**__,__ **[`Image`](#image)**__,__ **[`Animation`](#animation)**__,__ **[`Audio`](#audio)**__,__ **[`Synthesizer`](#synthesizer)**__.__ @@ -1682,6 +1682,52 @@ b'.\n..\nfile1.txt\nfile2.txt\n' ``` +JSON +---- +```python +import json + = json.dumps(, ensure_ascii=True, indent=None) + = json.loads() +``` + +### Read Object from JSON File +```python +def read_json_file(filename): + with open(filename, encoding='utf-8') as file: + return json.load(file) +``` + +### Write Object to JSON File +```python +def write_to_json_file(filename, an_object): + with open(filename, 'w', encoding='utf-8') as file: + json.dump(an_object, file, ensure_ascii=False, indent=2) +``` + + +Pickle +------ +```python +import pickle + = pickle.dumps() + = pickle.loads() +``` + +### Read Object from File +```python +def read_pickle_file(filename): + with open(filename, 'rb') as file: + return pickle.load(file) +``` + +### Write Object to File +```python +def write_to_pickle_file(filename, an_object): + with open(filename, 'wb') as file: + pickle.dump(an_object, file) +``` + + CSV --- **Text file format for storing spreadsheets.** @@ -1810,52 +1856,6 @@ db = connector.connect(host=, user=, password=, database=) ``` -JSON ----- -```python -import json - = json.dumps(, ensure_ascii=True, indent=None) - = json.loads() -``` - -### Read Object from JSON File -```python -def read_json_file(filename): - with open(filename, encoding='utf-8') as file: - return json.load(file) -``` - -### Write Object to JSON File -```python -def write_to_json_file(filename, an_object): - with open(filename, 'w', encoding='utf-8') as file: - json.dump(an_object, file, ensure_ascii=False, indent=2) -``` - - -Pickle ------- -```python -import pickle - = pickle.dumps() - = pickle.loads() -``` - -### Read Object from File -```python -def read_pickle_file(filename): - with open(filename, 'rb') as file: - return pickle.load(file) -``` - -### Write Object to File -```python -def write_to_pickle_file(filename, an_object): - with open(filename, 'wb') as file: - pickle.dump(an_object, file) -``` - - Bytes ----- **Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.** diff --git a/index.html b/index.html index d4b60b8..b2cacde 100644 --- a/index.html +++ b/index.html @@ -208,13 +208,13 @@ pre.prettyprint { -

Comprehensive Python Cheatsheet

Comprehensive Python Cheatsheet


#Contents

ToC = {
     '1. Collections': [List, Dictionary, Set, Tuple, Range, Enumerate, Iterator, Generator],
     '2. Types':       [Type, String, Regular_Exp, Format, Numbers, Combinatorics, Datetime],
     '3. Syntax':      [Args, Inline, Closure, Decorator, Class, Duck_Types, Enum, Exceptions],
     '4. System':      [Print, Input, Command_Line_Arguments, Open, Path, Command_Execution],
-    '5. Data':        [CSV, SQLite, JSON, Pickle, Bytes, Struct, Array, MemoryView, Deque],
+    '5. Data':        [JSON, Pickle, CSV, SQLite, Bytes, Struct, Array, MemoryView, Deque],
     '6. Advanced':    [Threading, Operator, Introspection, Metaprograming, Eval, Coroutine],
     '7. Libraries':   [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile,
                        NumPy, Image, Animation, Audio, Synthesizer]
@@ -1516,6 +1516,36 @@ shutil.copytree(from, to)          # Copies the entir
 0
 
+

#JSON

import json
+<str>    = json.dumps(<object>, ensure_ascii=True, indent=None)
+<object> = json.loads(<str>)
+
+ +

Read Object from JSON File

def read_json_file(filename):
+    with open(filename, encoding='utf-8') as file:
+        return json.load(file)
+
+ +

Write Object to JSON File

def write_to_json_file(filename, an_object):
+    with open(filename, 'w', encoding='utf-8') as file:
+        json.dump(an_object, file, ensure_ascii=False, indent=2)
+
+ +

#Pickle

import pickle
+<bytes>  = pickle.dumps(<object>)
+<object> = pickle.loads(<bytes>)
+
+ +

Read Object from File

def read_pickle_file(filename):
+    with open(filename, 'rb') as file:
+        return pickle.load(file)
+
+ +

Write Object to File

def write_to_pickle_file(filename, an_object):
+    with open(filename, 'wb') as file:
+        pickle.dump(an_object, file)
+
+

#CSV

Text file format for storing spreadsheets.

import csv
 
@@ -1626,36 +1656,6 @@ db = connector.connect(host=<str>, user=<str>, password=<str>,
-

#JSON

import json
-<str>    = json.dumps(<object>, ensure_ascii=True, indent=None)
-<object> = json.loads(<str>)
-
- -

Read Object from JSON File

def read_json_file(filename):
-    with open(filename, encoding='utf-8') as file:
-        return json.load(file)
-
- -

Write Object to JSON File

def write_to_json_file(filename, an_object):
-    with open(filename, 'w', encoding='utf-8') as file:
-        json.dump(an_object, file, ensure_ascii=False, indent=2)
-
- -

#Pickle

import pickle
-<bytes>  = pickle.dumps(<object>)
-<object> = pickle.loads(<bytes>)
-
- -

Read Object from File

def read_pickle_file(filename):
-    with open(filename, 'rb') as file:
-        return pickle.load(file)
-
- -

Write Object to File

def write_to_pickle_file(filename, an_object):
-    with open(filename, 'wb') as file:
-        pickle.dump(an_object, file)
-
-

#Bytes

Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.

<bytes> = b'<str>'                       # Only accepts ASCII characters and \x00 - \xff.
 <int>   = <bytes>[<index>]               # Returns int in range from 0 to 255.
 <bytes> = <bytes>[<slice>]               # Returns bytes even if it has only one element.
diff --git a/parse.js b/parse.js
index f0a3054..667ac23 100755
--- a/parse.js
+++ b/parse.js
@@ -23,7 +23,7 @@ const TOC =
   '    \'2. Types\':       [Type, String, Regular_Exp, Format, Numbers, Combinatorics, Datetime],\n' +
   '    \'3. Syntax\':      [Args, Inline, Closure, Decorator, Class, Duck_Types, Enum, Exceptions],\n' +
   '    \'4. System\':      [Print, Input, Command_Line_Arguments, Open, Path, Command_Execution],\n' +
-  '    \'5. Data\':        [CSV, SQLite, JSON, Pickle, Bytes, Struct, Array, MemoryView, Deque],\n' +
+  '    \'5. Data\':        [JSON, Pickle, CSV, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' +
   '    \'6. Advanced\':    [Threading, Operator, Introspection, Metaprograming, Eval, Coroutine],\n' +
   '    \'7. Libraries\':   [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile,\n' +
   '                       NumPy, Image, Animation, Audio, Synthesizer]\n' +
diff --git a/web/python-cheatsheet-d3a72f9.pdf b/web/python-cheatsheet-d3a72f9.pdf
deleted file mode 100644
index 2ef5e64..0000000
Binary files a/web/python-cheatsheet-d3a72f9.pdf and /dev/null differ