Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

#!/usr/local/bin/python 

# encoding: utf-8 

""" 

csvtools.py 

=============== 

:Summary: 

    A collection of functions and classes to help work with csv files 

 

:Author: 

    David Young 

 

:Date Created: 

    June 24, 2013 

 

:dryx syntax: 

    - ``_someObject`` = a 'private' object that should only be changed for debugging 

 

:Notes: 

    - If you have any questions requiring this script please email me: d.r.young@qub.ac.uk 

 

:Tasks: 

    - [ ] when complete, extract all code out of the main function and add cl commands 

    - [ ] make internal function private 

    - [ ] pull all general functions and classes into dryxPythonModules 

""" 

################# GLOBAL IMPORTS #################### 

import sys 

import os 

 

###################################################### 

# MAIN LOOP - USED FOR DEBUGGING OR WHEN SCRIPTING   # 

###################################################### 

def main(): 

    """ 

    The main function used when ``csvtools.py`` run as a single script from the cl 

    """ 

    ########## PRE-IMPORT SETUP ########## 

    relativePathToProjectRoot = "../../../" 

    import dryxPython.projectsetup as dps 

    projectSetup = dps.projectSetup( 

        dbConn=False, 

        relativePathToProjectRoot=relativePathToProjectRoot 

    ) 

    global settings, contentPaths 

    dbConn, log, settings, contentPaths = projectSetup.get_project_atrributes() 

 

    ########## IMPORTS ########## 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

    import dryxPython.commonutils as cu 

 

    ## START LOGGING ## 

    startTime = cu.get_now_sql_datetime() 

    log.info('--- STARTING TO RUN THE csvtools.py AT %s' % (startTime,)) 

 

    ## SET GLOBAL VARIABLES 

 

    ## WRITE CODE HERE 

 

    if dbConn: 

        dbConn.commit() 

        dbConn.close() 

    ## FINISH LOGGING ## 

    endTime = cu.get_now_sql_datetime() 

    runningTime = cu.calculate_time_difference(startTime, endTime) 

    log.info('-- FINISHED ATTEMPT TO RUN THE csvtools.py AT %s (RUNTIME: %s) --' % (endTime, runningTime, )) 

 

    return 

 

################################################################### 

# CLASSES                                                         # 

################################################################### 

 

################################################################### 

# PUBLIC FUNCTIONS                                                # 

################################################################### 

## LAST MODIFIED : June 14, 2013 

## CREATED : June 14, 2013 

## AUTHOR : DRYX 

def convert_csv_file_to_python_list_of_dictionaries( 

        log, 

        csvFilePath, 

        delimiter="|"): 

    """Convert a CSV file to a python list of dictionaries {"columnHeader": "value"} 

 

    **Key Arguments:** 

        - ``log`` -- logger 

        - ``csvFilePath`` -- path to the the csv file 

        - ``delimiter`` -- the csv delimiter 

 

    **Return:** 

        - ``dictionaryList`` -- list of dictionaries containing data from the csv file 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    import csv 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

 

    ################ >ACTION(S) ################ 

    try: 

        log.debug("attempting to open and read the csv file into a python list") 

        with open(csvFilePath, 'rb') as csvFile: 

            csvFileContents = csv.reader(csvFile, delimiter=delimiter) 

            dictionaryList = [] 

 

            headerList = csvFileContents.next() 

            # csvFileContents = csvFileContents[1:] 

 

            for row in csvFileContents: 

                thisDictionary = {} 

                for i in range(len(row)): 

                    thisDictionary[headerList[i].strip()] = row[i].strip() 

                dictionaryList.append(thisDictionary) 

 

        csvFile.closed 

    except Exception, e: 

        log.error("could not open and read the csv file into a python list - failed with this error: %s " % (str(e),)) 

        return -1 

 

    return dictionaryList 

 

## LAST MODIFIED : June 21, 2013 

## CREATED : June 21, 2013 

## AUTHOR : DRYX 

def convert_python_list_of_dictionaries_to_csv( 

        listOfDictionaries, 

        csvFilePath, 

        log): 

    """Converts a python list of dictionaries into a csv file with header = dictionary keys. 

 

    **Key Arguments:** 

        - ``listOfDictionaries`` -- the list of dictionaries { csvHeader : value } 

        - ``csvFilePath`` -- the path of the file to export the content to as a csv file 

        - ``log`` -- logger 

 

    **Return:** 

        - None 

 

    **Todo** 

    - [ ] when complete, clean convert_python_module_content_to_autoSnippet_csv function & add logging 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    import csv 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

 

    log.info('starting the ``convert_python_module_content_to_autoSnippet_csv`` function') 

    ## VARIABLES ## 

 

    with open(csvFilePath, 'wb') as csvfile: 

        writer = csv.writer( 

            csvfile, 

            dialect= 'excel' 

        ) 

        if len(listOfDictionaries) > 0: 

            writer.writerow(listOfDictionaries[0].keys()) 

            for dictionary in listOfDictionaries: 

                writer.writerow(dictionary.values()) 

    csvfile.close() 

 

    log.info('completed the ``convert_python_module_content_to_autoSnippet_csv`` function') 

    return 

 

################################################################### 

# PRIVATE (HELPER) FUNCTIONS                                      # 

################################################################### 

 

############################################ 

# CODE TO BE DEPECIATED                    # 

############################################ 

 

if __name__ == '__main__': 

    main() 

 

 

################################################################### 

# TEMPLATE FUNCTIONS                                              # 

###################################################################