Reputation: 15
I have generated a csv file and it looks like this:
50,57,13,10,50,48,13,10,49,55,13,10,49,54,13,10,49,52,13,10,49,52,13,10,49,50,13,10,49,49,13,10
49,49,13,10,57,13,10,57,13,10,57,13,10,56,13,10,56,13,10,55,13,10,54,13,10,54,13,10,54,13,10,54
13,10,54,13,10,54,13,10,54,13,10,53,13,10,54,13,10,54,13,10,54,13,10,54,13,10,54,13,10,53,13,10
53,13,10,52,13,10,52,13,10,52,13,10,53,13,10,53,13,10,53,13,10,52,13,10,51,13,10,52,13,10,52,13
10,52,13,10,53,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,53,13,10
53,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13
10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10
52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,50,13,10,51,13,10,51,13
10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,53,13,10,52,13,10
52,13,10,51,13,10,52,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13
I want to reframe it to such that it should have one row only and no columns at all. I tried numpy.genfromtxt
new=np.genfromtxt('repaired.csv', dtype='float', delimiter=',', skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars='"', replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)
but it didn't work. I am geting error as:
ValueError: Some errors were detected !
Line #1591 (got 28 columns instead of 32)
Line #1593 (got 4 columns instead of 32)
Upvotes: 0
Views: 64
Reputation: 1709
I have done all this way to convert the csv
file into numpy array
import csv
import numpy as np
data_in_csv_file = []
# reading the csv file
with open('hello2.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
data_in_csv_file.append(row)
# removing the empty row of the csv file
# and convert into a list
list_values = sum(data_in_csv_file,[])
# converting numpy array
values = np.array(list_values)
print(values)
Upvotes: 0
Reputation: 46899
if you have the possibility to use pandas
you can try this:
import pandas
new = pandas.read_csv('repaired.csv', sep=',', engine='python', header=None)
the first line in your csv must be amongst the longest lines though, otherwise this will not work either.
if you need your data as pure numpy
array you can convert it:
nm = new.as_matrix()
Upvotes: 1