lexical error: invalid bytes in UTF8 string

前端 未结 1 1907
臣服心动
臣服心动 2021-01-29 01:18

I am trying to use the code shown below to extract data from a json file. However, the following error is returned:

Error: lexical error: invalid bytes in UTF8          


        
相关标签:
1条回答
  • 2021-01-29 02:00

    The problem is that the URL returns data in a latin1 encoding, and your system is defaulting to reading it as UTF-8. You can get it correctly using

    library(jsonlite)
    library(RCurl)  
    
    URL <- "https://www.energy-charts.de/power_unit/month_lignite_unit_2017_12.json"
    
    data <- fromJSON(getURL(URL, encoding = "latin1"))
    

    I've also corrected some minor errors in your code: you forgot to request RCurl, and paste0 was not needed.

    0 讨论(0)
提交回复
热议问题