Skip to content

Instantly share code, notes, and snippets.

@yoki
Last active November 8, 2022 07:59
Show Gist options
  • Save yoki/d490ef0bc2b3b72f6a746c0abe635117 to your computer and use it in GitHub Desktop.
Save yoki/d490ef0bc2b3b72f6a746c0abe635117 to your computer and use it in GitHub Desktop.
# csv.py: csv file io
# file.py: operation related to files
# directory.py: operation related to finding files and traveling directory
# http://chimera.labs.oreilly.com/books/1230000000393/ch06.html#_solution_94
#-----------------
# Read CSV
#-----------------
import csv
with open('stocks.csv') as f:
# or
with io.open(master_path + 'stock_mst.csv',encoding='cp932') as f:
f_csv = csv.DictReader(f)
for row in f_csv:
# process row
#----------------
-
# Write CSV
#-----------------
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
]
with open('stocks.csv','w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)
## listing files
from pathlib import Path
# current path
root_path = Path().resolve()
# file path
Path(__file__).parent.resolve()
#Listing subdirectories:
[x for x in p.iterdir() if x.is_dir()]
# python files
list(Path(path_string).glob('**/*.py'))
# Navigating
p = Path('/etc')
q = p / 'init.d' / 'reboot' /'reboot.sh'
q.parent
q.parents # => list of parents
# path components
q.name # => filename
q.suffix # => extension
# open text files.
p = Path('my_text_file')
p.write_text('Text file contents')
p.read_text()
# open general files:
with p.open() as f:
f.readlines()
# Read the entire file as a single string
with open('rawdata.txt', 'r') as f:
data = f.read()
# Iterate over the lines of the file
with open('somefile.txt', 'r') as f:
for line in f:
# process line
...
# write
f = open('out.txt', 'w')
f.write('aaa')
f.close()
# Write chunks of text data
with open('somefile.txt', 'wt') as f:
f.write(text1)
f.write(text2)
...
# Redirected print statement
with open('somefile.txt', 'wt') as f:
print(line1, file=f)
print(line2, file=f)
import json
with open('data/src/test.json') as f:
df = json.load(f)
with open('data/src/test.json', 'w') as f:
# disable ensure ascii to avoid escaping.
json.dump(df,f, ensure_ascii=False, indent=2)
import pickle
with open('singer.pickle', 'wb') as f:
pickle.dump(singer, f)
with open('singer.pickle', 'rb') as f:
obj = pickle.load(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment