0

I'm using python (np.loadtxt) to open text file which is created in windowsOS, and trying to use it in macOS. The python script is working just fine on my windows, but it is giving me UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 23: invalid start byte error when I try to use the same script on macbook. It seems like it's caused by file names, because the line this error occured is the line I'm reading text file.

for names in os.listdir(dir_2):
    array_txt = np.loadtxt(dir_2+"/"+names)
    x = array_txt[:800,0]
    y = array_txt[:800,1]

I googled it and tried str = unicode(str, errors='replace') on my file name string. But still got error that says name 'unicode' is not defined. I installed unidecode, and also tried importing unicodedata but couldn't fix it. I also tried creating text file on the macbook and copying the data from original file, but still got the same error. I also found someone saying it's a bug in numpy, but I'm not sure if I'm experiencing the bug or is it something different. Can someone give explanations? Thank you in advance.

Amur Koko
  • 11
  • 3
  • 1
    Does this answer your question? [UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte](https://stackoverflow.com/questions/22216076/unicodedecodeerror-utf8-codec-cant-decode-byte-0xa5-in-position-0-invalid-s) – v.tralala Feb 10 '21 at 05:22
  • 1
    There is an `encoding` parameter to `loadtxt`. The default encoding between Windows and MacOS is likely different. The file is not UTF-8, so try the typical Windows default of `encoding='cp1252'`. 0x87 would be the character `‡` in that encoding, if that makes sense in your data. Having a sample data file to reproduce the issue would help. – Mark Tolonen Feb 10 '21 at 05:47
  • @v.tralala thank you for your answer, i looked at thread before, but didn't even know if it helps me or not :( – Amur Koko Feb 12 '21 at 04:57
  • @MarkTolonen thank you for your answer, i didn't know about loadtxt parameters. I tried adding that parameter to function but got completely different errors and output. I uploaded screenshot of my code, and error. https://ibb.co/m0fXqF9 – Amur Koko Feb 12 '21 at 04:59

0 Answers0