Question

I'm trying to do something simple but for some reason I cannot. I have been looking for related answers in the site but I haven't been able to accomplish.

So, I'm trying to import a text file, csv, or rtf to an np.array, so far I have got

example.cvs is a file with

881.00,882.00
883.00,884.00

>>> import numpy as np
>>> txtdata = np.genfromtxt('example.cvs',usecols=0, dtype='S51',delimiter=',') 

>>> txtdata
array(['{\\rtf1\\ansi\\ansicpg1252\\cocoartf1265\\cocoasubrtf190',
       '{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}',
       '{\\colortbl;\\red255\\green255\\blue255;}',
       '\\margl1440\\margr1440\\vieww10800\\viewh8400\\viewkind0',
       '\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx39',
       '\\f0\\fs24 \\cf0  881.00', '883.00'], 
      dtype='|S51')

I don't know why comes with all this stuff, I just want an array with those 4 numbers. A 2x2 Matrix.

Thanks

Was it helpful?

Solution

Apparently, you are reading an RTF file, rather than a plain text file. Also, your usecols and dtype parameters are wrong.

With a CSV file that you show you can just do:

>>> np.genfromtxt('example.csv', delimiter=',')
array([[ 881.,  882.],
       [ 883.,  884.]])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top