-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io.py
68 lines (46 loc) · 2.26 KB
/
file_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Module name: file_io.py
# Purpose: write / read csv files
#
# Notes:
#
# Copyright: 2019, release under GPL3. See LICENSE file for details
# Carl W Greenstreet <[email protected]>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# import libraries
import csv # i/o of csv files
import os # access files on drive
#import inspect # to get variable names
# import HT-analysis modules
import config # global variables
import debug # debug functions - variable values / types / counts
def csv_read(PATH, NAME):
"""reads a csv file into list called contents"""
with open(PATH + NAME, 'r') as f:
reader = csv.reader(f, delimiter=',')
contents = list(reader)
flat_list = [item for sublist in contents for item in sublist] #https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists
return flat_list
def csv_write(PATH, NAME, contents):
"""writes list called contents into a csv file"""
with open(PATH + NAME, 'a') as csvFile: #note: 'a' means open for writing - append
writer = csv.writer(csvFile)
writer.writerows([contents]) # https://stackoverflow.com/questions/15129567/csv-writer-writing-each-character-of-word-in-separate-column-cell
# writerow vs writerows: https://stackoverflow.com/questions/33091980/difference-between-writerow-and-writerows-methods-of-python-csv-module
csvFile.close()
# test code follows:
if __name__ == "__main__":
# Set debugging status: on=True or off=False
debug_flag = True
debug.status_msg(debug_flag)
# ---------------- Test CSV Read -----------------------------------
PATH = "/Users/carlwgreenstreet/Documents/Git/Heysen-Trail-Analysis/"
NAME = "tripidx.csv"
config.tripidx = csv_read(PATH, NAME)
debug.val_type(config.tripidx, debug_flag) # debug code: page numbers
debug.list_count(config.tripidx, debug_flag)
# ---------------- Test CSV Write -----------------------------------
NAME = "delete_test.csv"
csv_write(PATH, NAME, config.tripidx)