10 examples of 'load csv file in python' in Python

Every line of 'load csv file in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
149def load_from_csv(language, delimiter=','):
150 ''' Parse mapping from csv
151 '''
152 work_sheet = []
153 with open(language, encoding='utf8') as f:
154 reader = csv.reader(f, delimiter=delimiter)
155 for line in reader:
156 work_sheet.append(line)
157 # Create wordlist
158 mapping = []
159 # Loop through rows in worksheet, create if statements for different columns
160 # and append mappings to self.mapping.
161 for entry in work_sheet:
162 new_io = {"in": "", "out": "",
163 "context_before": "", "context_after": ""}
164 new_io['in'] = entry[0]
165 new_io['out'] = entry[1]
166 try:
167 new_io['context_before'] = entry[2]
168 except IndexError:
169 new_io['context_before'] = ''
170 try:
171 new_io['context_after'] = entry[3]
172 except IndexError:
173 new_io['context_after'] = ''
174 for k in new_io:
175 if isinstance(new_io[k], float) or isinstance(new_io[k], int):
176 new_io[k] = str(new_io[k])
177 mapping.append(new_io)
178
179 return mapping
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

51def load_csv_file(csv_file_path):
52 lines = get_lines_from_csv_file(csv_file_path)
53
54 scores = []
55 for line in lines[1:]:
56 begin = 0
57 for strategy in STRATEGIES:
58 score = [strategy] + map(int, line[begin: begin + 4])
59 begin += 4
60 scores.append(score)
61
62 return scores, CLASSIFICATIONS
6def load_csv(data):
7 sio = StringIO(data)
8 return list(unicodecsv.DictReader(sio, encoding="utf-8"))
50def load_csv(file):
51 flair_dict = []
52 print "start"
53 with open(file) as flair:
54 for line in flair:
55 username, flair_css, flair_text = line.rstrip().split(',')
56 print("Setting username: \'" + username + "\' to flair_css_class: \'" + flair_css + "\' and flair_text to: \'" + flair_text + "\'")
57 i = {
58 'user': username,
59 'flair_css_class': flair_css,
60 'flair_text': flair_text,
61 }
62 flair_dict.append(i)
63
64 return flair_dict
23def load_csv_data(csv_file):
24 import csv
25 categories = []
26 comments = []
27 with open(csv_file, 'rb') as raw_dataset:
28 dataset_reader = csv.reader(raw_dataset, delimiter=',')
29 next(dataset_reader, None) # Filter Header
30 for row in dataset_reader:
31 categories.append(int(row[0]))
32 comments.append(preprocess_comment(row[2]))
33 return categories, comments
77def load_file(file, relevant): # FIXME: must rewrite
78 reader = csv.reader(open(file))
79
80 headings = reader.next()
81 columns_to_keep = [headings.index(col) for col in relevant]
82
83 objects = []
84 for row in reader:
85 #print row[1]
86 data = []
87 for ix in columns_to_keep:
88 try:
89 data.append(row[ix])
90 except IndexError:
91 data.append(None) # we don't have a value
92 objects.append(data)
93
94 return objects
15def _load(self):
16 logger.debug(f'load csv from {self.path}')
17 self.df = pd.read_csv(self.path)
18 logger.debug(f'load csv from {self.path} success')
102def load(self):
103 for tbl in Gtfs.TABLES:
104 setattr(Gtfs, tbl['getter'], self.make_getter(tbl['obj'], tbl['table'], optional=tbl.get('optional')))
105 return self
146def get_data_from_file(csv_content, files):
147 # Get description or fix from the file reference parsed in JsonToCsv class
148 data = ''
149 number_from_file = re.search('\d+', csv_content)
150 if not number_from_file:
151 return data
152 else:
153 file_number = number_from_file.group()
154
155 if file_number in files['filenames']:
156 filename = files['filenames'][file_number]
157 else:
158 return data
159
160 with open(path.join(files['path'], filename)) as file_object:
161 data = file_object.read()
162
163 return data
39@staticmethod
40def load_csv(filepath):
41 """Load .csv file into a dataframe object.
42
43 Arguments:
44 filepath {String} -- Path to source file.
45
46 Returns:
47 DataFrame -- Returns a dataframe object.
48 """
49 return pd.read_csv(filepath, encoding="utf-8")

Related snippets