# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Questions
# * Check the import data section of data-camp data science career track for python.
#
# ## file IO with 
#

# %%writefile file1.py
def myfunc(x):
    return [num for num in range(x) if num%2==0]
list1 = myfunc(11)
# Make sure the syntax of following stuff: %%, writefile.
# Note I cannot write comments before two %%. 

# +

import csv

all_chords = ['C', 'C', 'E', 'G']
myfile = open('C_chords.csv','w')
wr = csv.writer(myfile, delimiter=',')
myfile.close()

#Open in context manager
with open ('companies', 'rb') as fp:
     companies = pickle.load(fp)
     #This way does not need to explicitly close the opened file. However,sometimes we need open a file and 
     #and then read the file contents to a list, then we don't want close the file first.
     #Thus we need read the file in the current indentation level. Otherwise, the file is already close.

    
#Another example of opening in context manager
with engine.connect() as con:
    rs = con.execute("SELECT LastName, Title FROM Employee")
    df = pd.DataFrame(rs.fetchmany(size=3))
    df.columns = rs.keys()

print(len(df))

print(df.head())
# -

# ## file IO with Pandas
# * This should be the most important IO scheme for data science. See another file for DataFrameCreation. 

import pandas as pd
df = pd.read_csv('filename')

# ## file IO with Numpy
#
# In fact, I can use pandas to replace this. I can just read in a csv file with pd.read_csv and use the .values attribute to directly assign to np array. So the following is not critical. Knowing it is enough. 
#
# If all data in csv file are numbers of array, use np.loadtext, we obtain directly np.array, without loading as dataframe and takes values as np array.
#

import numpy as np
samples = np.loadtxt('seeds.csv',delimiter = ',')
np.savetxt('new_points.csv', new_points, delimiter=",")


# ## file IO with pickle
#
# **My Comments**: Unlike other approaches which is good for one format or other, this pickel method can handle either DataFrame in pandas, array in numpy, list in python. So whenever I just need output and then read in without readability for others, then picke is a general approach? And it save space?

import pandas as pd
import numpy as np
hello = pd.DataFrame({'d':[1,2,3],'e':['a', 'b', 'c']}) # don't forget the {}
#hello = [['1',2,3], '2', 'a']
#hello = np.array([1, 2 ,3])
print(hello.head())
import pickle
with open('C:\\Users\\ljyan\\Desktop\\courseNotes\\python\\Summaries\\temp', 'wb') as fp:
    pickle.dump(hello, fp)
with open ('temp', 'rb') as fp:
    x = pickle.load(fp)
# How does pickle know the type of x. In the pickle file there might be such informaiton?

StarcoderdataJupyterScriptsDedupFiltered