10 examples of 'python read csv' in Python

Every line of 'python read csv' 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
37def readcsv(filename, header=True):
38 return pd.read_csv(filename, header=None) if not header else pd.read_csv(filename)
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

34def test_read_csv():
35 io = FileIO()
36 filename = os.path.join(os.path.dirname(
37 os.path.abspath(__file__)),
38 'stock_N225.csv')
39 df = io.read_from_csv("N225", filename)
40
41 result = round(df.ix['2015-03-20', 'Adj Close'], 2)
42 expected = 19560.22
43 eq_(expected, result)
136def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
137 """ csv.py doesn't do Unicode; encode temporarily as UTF-8."""
138 csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
139 dialect=dialect, **kwargs)
140 for row in csv_reader:
141 # decode UTF-8 back to Unicode, cell by cell:
142 yield [unicode(cell, 'utf-8') for cell in row]
11def read_csv(array, filename):
12 with open(filename, 'rb') as input_file:
13 foo = csv.reader(input_file)
14 for row in foo:
15 array.append(row)
16 return array
67def read_csv(fh):
68 """Read the CSV input"""
69 exercises = []
70
71 for row in csv.DictReader(fh, delimiter=','):
72 name = row['exercise']
73 low, high = row['reps'].split('-')
74 exercises.append((name, int(low), int(high)))
75
76 return exercises
6def load_csv(data):
7 sio = StringIO(data)
8 return list(unicodecsv.DictReader(sio, encoding="utf-8"))
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
12def readFile():
13 #make the format of the csv file. Our format is a vector with 13 features and a label which show the condition of the
14 #sample hc/pc : helathy case, parkinson case
15 names = ['Feature1', 'Feature2', 'Feature3', 'Feature4','Feature5','Feature6','Feature7','Feature8','Feature9',
16 'Feature10','Feature11','Feature12','Feature13','Label']
17
18 #path to read the samples, samples consist from healthy subjects and subject suffering from Parkinson's desease.
19 path = ''
20 #read file in csv format
21 data = pd.read_csv(path,names=names )
22
23 #return an array of the shape (2103, 14), lines are the samples and columns are the features as we mentioned before
24 return data
20def readFile():
21 #make the format of the csv file. Our format is a vector with 13 features and a label which show the condition of the
22 #sample hc/pc : helathy case, parkinson case
23 names = ['Feature1', 'Feature2', 'Feature3', 'Feature4','Feature5','Feature6','Feature7','Feature8','Feature9',
24 'Feature10','Feature11','Feature12','Feature13','Label']
25
26 #path to read the samples, samples consist from healthy subjects and subject suffering from Parkinson's desease.
27 path = 'mfcc_multiclass.txt'
28 #read file in csv format
29 data = pd.read_csv(path,names=names )
30
31 #return an array of the shape (2103, 14), lines are the samples and columns are the features as we mentioned before
32 return data
41def read_csv_file(file_name):
42 """
43 Return patient ID and sinogram array produced by reading a RayStation sinogram
44 CSV file with the provided file name.
45
46 Files are produced by ExportTomoSinogram.py, Brandon Merz,
47 RaySearch customer forum, 1/18/2018.
48
49 Format:
50 First row contains demographics. Subsequent rows correspond to couch positions.
51 Leaf-open time range from zero to one.
52 "Patient name: ANONYMOUS^PATIENT, ID: 00000",,,,,,,,,
53 ,0,0,0,0,0,0,0,0,0,0,0,0,0.39123373,0.366435635 ...
54 """
55
56 with open(file_name, 'r') as csvfile:
57
58 pat_name, pat_num = csvfile.readline().split('ID:')
59 pat_name = pat_name.replace('Patient name:', '')
60
61 pat_name_last, pat_name_first = pat_name.split('^')
62 pat_name_last = ''.join([c for c in pat_name_last if c in LETTERS])
63 pat_name_first = ''.join([c for c in pat_name_first if c in LETTERS])
64 pat_num = ''.join([c for c in pat_num if c in DIGITS])
65
66 document_id = pat_num + ' - ' + pat_name_last + ', ' + pat_name_first
67 reader = csv.reader(csvfile, delimiter=',')
68 array = np.asarray([line[1:] for line in reader]).astype(float)
69
70 return document_id, array

Related snippets