Python Programming Tutorials ( BASIC + ADVANCE ): Learn From Scratch With Examples by Rahul Maurya

Python Programming Tutorials ( BASIC + ADVANCE ): Learn From Scratch With Examples by Rahul Maurya

Author:Rahul Maurya [Maurya, Rahul]
Language: eng
Format: azw3, epub
Publisher: UNKNOWN
Published: 2020-06-29T16:00:00+00:00


✔ Learn to use Python professionally, learning both Python 2 and Python 3!

✔ Create games with Python, like Tic Tac Toe and Blackjack!

✔ Learn advanced Python features, like the collections module and how to work with timestamps! ✔ Learn to use Object Oriented Programming with classes!

✔ Understand complex topics, like decorators.

✔ Understand how to use both the Jupyter Notebook and create .py files

✔ Get an understanding of how to create GUIs in the Jupyter Notebook system!

✔ Build a complete understanding of Python from the ground up!

Watch Today →

Chapter 105: Writing to CSV from String or List

Parameter open ("/path/", "mode") open (path, "mode") csv.writer(file, delimiter)

Details

Specify the path to your CSV file

Specify mode to open file in (read, write, etc.) Pass opened CSV file here

csv.writer(file, delimiter=' ')Specify delimiter character or pattern

Writing to a .csv file is not unlike writing to a regular file in most regards, and is fairly straightforward. I will, to the best of my ability, cover the easiest, and most efficient approach to the problem.

Section 105.1: Basic Write Example

import csv

#------ We will write to CSV in this function -----------

def csv_writer(data , path):

#Open CSV file whose path we passed.

with open (path , "wb" ) as csv_file:

writer = csv .writer(csv_file , delimiter = ',' ) for line in data:

writer.writerow(line)

#---- Define our list here, and call function -----------

if __name__ == "__main__" :

"""

data = our list that we want to write. Split it so we get a list of lists.

"""

data = ["first_name,last_name,age" .split("," ),

"John,Doe,22" .split("," ),

"Jane,Doe,31" .split("," ),

"Jack,Reacher,27" .split("," )

]

# Path to CSV file we want to write to. path = "output.csv"

csv_writer(data, path)

Section 105.2: Appending a String as a newline in a CSV file

def append_to_csv(input_string):

with open ( "fileName.csv" , "a" ) as csv_file:

csv_file.write(input_row + " \n " )

Chapter 106: Dynamic code execution with `exec` and `eval`

Argument Details

expressionThe expression code as a string, or a code object

object The statement code as a string, or a code object

globals

The dictionary to use for global variables. If locals is not specified, this is also used for locals. If omitted, the globals () of calling scope are used.

A mapping object that is used for local variables. If omitted, the one passed for globals is used locals instead. If both are omitted, then the globals () and locals () of the calling scope are used for

globals and locals respectively.

Section 106.1: Executing code provided by untrusted user using exec, eval, or ast.literal_eval

It is not possible to use eval or exec to execute code from untrusted user securely. Even ast.literal_eval is prone to crashes in the parser. It is sometimes possible to guard against malicious code execution, but it doesn't exclude the possibility of outright crashes in the parser or the tokenizer.

To evaluate code by an untrusted user you need to turn to some third-party module, or perhaps write your own parser and your own virtual machine in Python.

Section 106.2: Evaluating a string containing a Python literal with ast.literal_eval

If you have a string that contains Python literals, such as strings, floats etc, you can use ast.literal_eval to evaluate its value instead of eval .



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.