Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
Created February 15, 2019 15:52
Show Gist options
  • Save blaylockbk/55b5b98269a4f1a413f6b4e676261fa6 to your computer and use it in GitHub Desktop.
Save blaylockbk/55b5b98269a4f1a413f6b4e676261fa6 to your computer and use it in GitHub Desktop.
numpy genfromtxt mixed numbers and strings encoding
# For a .csv file where there are mixed data types (column of floats, column of strings i.e. Dates)
# then if you set the encoding='UTF-8', the dates or column of strings will return as a string
# rather than a "bytes" type.
FILENAME = 'some_csv_file.csv'
data = np.genfromtxt(FILENAME, delimiter=',', names=True, dtype=None, encoding='UTF-8')
# Since names is set to True, the returned data can be accessed by header column name.
# If a column is labeled 'DATES', you can access the dates like you do a dictionary...
get_dates = data['DATES']
# The thing is, the data variable type is a "numpy.void".
# You can get a list of names like this...
names = data.dtype.names
# Print all data from each name:
for i in data.dtype.names:
print(i, data[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment