Here are some file reading methods that can be used with python. Since it is for when what kind of function was there, detailed explanation of each function is omitted.
open()
Specify the file and mode.
The default mode is 'r'
(read-only)
main.py
#When reading and displaying line by line
data = open('/path/to/data', 'r')
for line in data:
print line
data.close()
#When reading and displaying all contents
data = open('/path/to/data', 'r')
all_data = data.read()
print all_data
data.close()
read_csv(), read_table() pandas Read_csv () for comma separated, read_table () for tab separated What you read is returned as a data frame
main.py
import pandas as pd
data_csv = pd.read_csv('/path/to/data.csv')
data_tsv = pd.read_table('/path/to/data.tsv')
loadtxt() numpy Also specify the delimiter and type (default is space delimiter and `` `float```) What you read is returned as an array
main.py
import numpy as np
data = np.loadtxt('/path/to/data', delimiter=' ', dtype = 'float')
Recommended Posts