Skip to content

Instantly share code, notes, and snippets.

@dettmering
Created September 22, 2012 18:43
Show Gist options
  • Save dettmering/3767366 to your computer and use it in GitHub Desktop.
Save dettmering/3767366 to your computer and use it in GitHub Desktop.
Python: Read CSV file into array
def readcsv(filename):
ifile = open(filename, "rU")
reader = csv.reader(ifile, delimiter=";")
rownum = 0
a = []
for row in reader:
a.append (row)
rownum += 1
ifile.close()
return a
@PratyushTripathy
Copy link

I think a better way would be to use Pandas using the following function:
def readcsv(filename):
data = pd.read_csv(filename) #Please add four spaces here before this line
return(np.array(data)) #Please add four spaces here before this line

yourArray = readcsv(yourFilename)
Good Luck!

@thejose5
Copy link

This reads each row as a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment