I have a csv file and I have to read this in jupyter notebook with np.genfromtxt(). (Unfortunately, I cannot use pd.read_csv() and even other pandas functions or methods.) Instead of showing you the file, let me give you an example. The file is as below:
Item,Color,Price
iPhone,Red,"1,000"
Galaxy,Black,"1,100"
I made a code that can read such csv file as:
def csv_opener(path):
with open(path, 'r') as f:
first_line = f.readline()
num_col = len(first_line.split(','))
return np.genfromtxt(path, delimiter=',', dtype=None, usecols=range(num_col))
You can see there are commas in Price values. But since delimiter is set as a comma, the "csv_opener" function reads the file as:
iPhone | Red | '"1' | '000"'
Here are the questions.
- How can I make my "csv_opener" function recognize double-quoted value as one? (not '"1' | '000"' -> '1,000')
- How can I change the data type after reading them? (not '1,000' -> numeric types such as integer/float)