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 ?

14
  • 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) Commented Feb 2, 2018 at 4:20
  • try encoding='utf-8' Commented Feb 2, 2018 at 4: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. Commented Feb 2, 2018 at 4:23
  • 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. Commented Feb 2, 2018 at 7:57
  • 1
    see if this helps. Commented Feb 2, 2018 at 7:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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; discussions.apple.com/thread/4167629 and see also apple.stackexchange.com/questions/84309/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.