0
import matplotlib.pyplot as plt
import numpy as np

x, y = np.loadtxt('num.txt', delimiter=',', unpack=True)
plt.plot(x,y, label='Loaded from file!')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()

whenever I run this code, it shows me this error

UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 11: ordinal not in range(128)".

how to open files in python on mac os ?

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
Aravind Sairam
  • 101
  • 1
  • 1
  • 8
  • 1
    loadtxt is probably for txt-files. Is it a text-file? Looks like a binary-file in regards to that error. Check it! (naive way: open it in an text-editor and look for garbage-like output) – sascha Feb 02 '18 at 04:20
  • try encoding='utf-8' – whackamadoodle3000 Feb 02 '18 at 04:22
  • 1
    Python is trying to help you -- that error message is pretty descriptive. I suggest you try googling it, because I can tell you that your problem is not opening files, seeing as your program is trying to encode characters read from that file. – cmaher Feb 02 '18 at 04:23
  • tired of googling but the solutions are not clear. – Aravind Sairam Feb 02 '18 at 04:42
  • @AravindSairam open a terminal, `cd` to the folder where your `num.txt` is and type `cat num.txt` -- do you see nicely arranged numbers or really cryptic looking characters? – Thomas Kühn Feb 02 '18 at 07:04
  • What is the source of that file? Can you please provide us with a sample file? – Hameer Abbasi Feb 02 '18 at 07:20
  • the content in the file is 1,9 2,8 3,7 4,6 5,5 6,4 7,3 8,2 9,1 – Aravind Sairam Feb 02 '18 at 07:51
  • The file apparently *isn't* UTF-8. See e.g. https://stackoverflow.com/questions/48259515/decoding-issue-while-parsing-json-python for some troubleshooting ideas. – tripleee Feb 02 '18 at 07:52
  • Please [edit] your question to update it with your sample; posting a sample here in the comments obviously isn't working very well, and the question should be self-contained anyway. – tripleee Feb 02 '18 at 07:54
  • @ThomasKühn i can't do that command.....bcoz in mac .txt file can't be opened it is actually in .pages format – Aravind Sairam Feb 02 '18 at 07:54
  • @tripleee ok bro – Aravind Sairam Feb 02 '18 at 07:56
  • 1
    Python has no idea how to parse a Pages file. You need to export or otherwise pick apart the file and export the data in a form which Python can read. – tripleee Feb 02 '18 at 07:57
  • 1
    see if [this](https://stackoverflow.com/a/24235106/2454357) helps. – Thomas Kühn Feb 02 '18 at 07:57
  • On my computer, a Pages file seems to be actually a zip file containing a number of members. The `Index/Document.iwa` member seems to contain the text from the page, but it's in there with a bunch of other binary gunk so extracting information from there is not straightforward. – tripleee Feb 02 '18 at 08:01

1 Answers1

1

The file you are attempting to open is not a text file. Python, and Numpy, doesn't directly support reading a structured file in an unknown format.

If you can find a library or program which can extract the data from Pages into a text file or Python string, your code should work on the result. (Here is one possible lead: https://github.com/obriensp/iWorkFileFormat/blob/master/Docs/index.md#iwa)

Pages is a layout program; there is no guarantee that a Pages file contains any text at all, much less any particular text block which is suitable as input for Numpy. You have to find a way to extract the information you want, programmatically or not. Unless you need to do this in bulk (enough to make a case for significant developer effort), my suggestion would be to simply copy/paste the chunk you want into a text file manually.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Confusingly, the MacOS TextEdit app does *not* offer the option to produce text files out of the box. There is apparently a preference you can change; https://discussions.apple.com/thread/4167629 and see also https://apple.stackexchange.com/questions/84309/how-to-create-a-text-file-in-a-folder – tripleee Feb 02 '18 at 08:14