1

I want to load a txt file into a numpy array. The file has this format:

1,10,1,11,1,13,1,12,1,1,9
2,11,2,13,2,10,2,12,2,1,9
3,12,3,11,3,13,3,10,3,1,9
4,10,4,11,4,1,4,13,4,12,9
4,1,4,13,4,12,4,11,4,10,9
1,2,1,4,1,5,1,3,1,6,8
1,9,1,12,1,10,1,11,1,13,8
2,1,2,2,2,3,2,4,2,5,8
3,5,3,6,3,9,3,7,3,8,8
4,1,4,4,4,2,4,3,4,5,8
.
.
.

a width of 11 values and a height of 25010 (so I don't want to put them manually into the array)

I already tried load, loadtxt, loadfile and genfromtxt. All of them failed. I'm pretty sure the commas are the problem.

You have a solution?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

9

You need to specify the delimiter as ,:

import numpy as np

data = np.loadtxt("file.txt", delimiter=",")
Tim B
  • 3,033
  • 1
  • 23
  • 28
  • @jackjuni No problem, if this answers your question please mark it as "accepted" (the tick mark under the voting arrows). – Tim B Mar 17 '17 at 12:51
  • It wasn't possible, because he answered so fast. A little red notify popped up which said that I have to wait 8 minutes before I can accept. –  Mar 17 '17 at 13:05