te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Loading Json file in to Python "UnicodeDecodeError ... invalid continuation byte" - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loading Json file in to Python "UnicodeDecodeError ... invalid continuation byte" - Stack Overflow

programmeradmin3浏览0评论

I am new to Python/Json. My end goal is to load a json file. From the json file info I want to get it into a database.

My thought is to bring the json file in to python and save it as a dictionary.(I'm sure there are quicker/better ways). Then create a database and use the dictionary information to create columns with data.

So, my issue is I am trying to load a json file but I keep getting the following error. I have tried multiple changes/versions to input the file, but I can never succeed.

Here is my code and the errors:

>>> with open("test11.json","r") as file:
...     data = json.load(file)
...     print(data)
...     
Traceback (most recent call last):
  File "<python-input-11>", line 2, in <module>
    data = json.load(file)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 293, in load
    return loads(fp.read(),
                 ~~~~~~~^^
  File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 24: invalid continuation byte
>>> 

Here is my json file(I have use an online json site to verify that my code is valid):

{
    "activityId": 18132401113,
    "uuidMsb": 817921559470751700,
    "uuidLsb": -7059629815279243000,
    "name": "Indoor Cycling",
    "activityType": "indoor_cycling",
    "userProfileId": 1,
    "timeZoneId": 10,
    "beginTimestamp": 1738148659000,
    "eventTypeId": 9,
    "rule": "subscribers",
    "sportType": "CYCLING",
    "startTimeGmt": 1738148659000,
    "startTimeLocal": 1738127059000,
    "duration": 1554817.9931640625,
    "distance": 1000000,
    "avgSpeed": 0.643162093569794,
    "maxSpeed": 0.7194450200000001,
    "avgHr": 110,
    "maxHr": 123,
    "minHr": 72,
    "avgBikeCadence": 85,
    "maxBikeCadence": 92,
    "calories": 892.47426,
    "bmrCalories": 146.6507,
    "aerobicTrainingEffect": 2.0999999046325684,
    "avgFractionalCadence": 0,
    "maxFractionalCadence": 0,
    "maxFtp": 228,
    "elapsedDuration": 1554817.9931640625,
    "movingDuration": 0,
    "anaerobicTrainingEffect": 0,
    "deviceId": 1,
    "minTemperature": 28,
    "maxTemperature": 31,
    "manufacturer": "GARMIN",
    "lapCount": 1,
    "waterEstimated": 342,
    "trainingEffectLabel": "RECOVERY",
    "activityTrainingLoad": 28.710250854492188,
    "aerobicTrainingEffectMessage": "MINOR_AEROBIC_BENEFIT_0",
    "anaerobicTrainingEffectMessage": "NO_ANAEROBIC_BENEFIT_0",
    "moderateIntensityMinutes": 11,
    "vigorousIntensityMinutes": 14,
    "hrTimeInZone_0": 14446,
    "hrTimeInZone_1": 164002,
    "hrTimeInZone_2": 898000,
    "hrTimeInZone_3": 478370,
    "hrTimeInZone_4": 0,
    "hrTimeInZone_5": 0,
    "decoDive": false,
    "purposeful": false,
    "autoCalcCalories": false,
    "favorite": false,
    "pr": false,
    "elevationCorrected": false,
    "atpActivity": false,
    "parent": false
}

I am new to Python/Json. My end goal is to load a json file. From the json file info I want to get it into a database.

My thought is to bring the json file in to python and save it as a dictionary.(I'm sure there are quicker/better ways). Then create a database and use the dictionary information to create columns with data.

So, my issue is I am trying to load a json file but I keep getting the following error. I have tried multiple changes/versions to input the file, but I can never succeed.

Here is my code and the errors:

>>> with open("test11.json","r") as file:
...     data = json.load(file)
...     print(data)
...     
Traceback (most recent call last):
  File "<python-input-11>", line 2, in <module>
    data = json.load(file)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 293, in load
    return loads(fp.read(),
                 ~~~~~~~^^
  File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 24: invalid continuation byte
>>> 

Here is my json file(I have use an online json site to verify that my code is valid):

{
    "activityId": 18132401113,
    "uuidMsb": 817921559470751700,
    "uuidLsb": -7059629815279243000,
    "name": "Indoor Cycling",
    "activityType": "indoor_cycling",
    "userProfileId": 1,
    "timeZoneId": 10,
    "beginTimestamp": 1738148659000,
    "eventTypeId": 9,
    "rule": "subscribers",
    "sportType": "CYCLING",
    "startTimeGmt": 1738148659000,
    "startTimeLocal": 1738127059000,
    "duration": 1554817.9931640625,
    "distance": 1000000,
    "avgSpeed": 0.643162093569794,
    "maxSpeed": 0.7194450200000001,
    "avgHr": 110,
    "maxHr": 123,
    "minHr": 72,
    "avgBikeCadence": 85,
    "maxBikeCadence": 92,
    "calories": 892.47426,
    "bmrCalories": 146.6507,
    "aerobicTrainingEffect": 2.0999999046325684,
    "avgFractionalCadence": 0,
    "maxFractionalCadence": 0,
    "maxFtp": 228,
    "elapsedDuration": 1554817.9931640625,
    "movingDuration": 0,
    "anaerobicTrainingEffect": 0,
    "deviceId": 1,
    "minTemperature": 28,
    "maxTemperature": 31,
    "manufacturer": "GARMIN",
    "lapCount": 1,
    "waterEstimated": 342,
    "trainingEffectLabel": "RECOVERY",
    "activityTrainingLoad": 28.710250854492188,
    "aerobicTrainingEffectMessage": "MINOR_AEROBIC_BENEFIT_0",
    "anaerobicTrainingEffectMessage": "NO_ANAEROBIC_BENEFIT_0",
    "moderateIntensityMinutes": 11,
    "vigorousIntensityMinutes": 14,
    "hrTimeInZone_0": 14446,
    "hrTimeInZone_1": 164002,
    "hrTimeInZone_2": 898000,
    "hrTimeInZone_3": 478370,
    "hrTimeInZone_4": 0,
    "hrTimeInZone_5": 0,
    "decoDive": false,
    "purposeful": false,
    "autoCalcCalories": false,
    "favorite": false,
    "pr": false,
    "elevationCorrected": false,
    "atpActivity": false,
    "parent": false
}
Share Improve this question edited 2 days ago wjandrea 33.1k9 gold badges69 silver badges97 bronze badges asked Feb 18 at 1:43 WinrolWinrol 93 bronze badges 5
  • There seems to be something wrong with your file named test11.json. Your code works perfectly on my machine. I would try copying the json from your stack overflow question, writing it to a new file, saving it, and reading from that. Another possible issue is that whatever text editor you're using isn't actually saving the file as utf-8 which python is expecting. I have had this issue personally on a windows device. Try using VSCode, and set it to save as UTF-8. I don't think your problem is in your code and it's logic – Bailey Brightman Commented Feb 18 at 2:00
  • 2 Try specifying an encoding like cp1252 or latin-1 – JonSG Commented Feb 18 at 2:11
  • 1 How was the file test11.json created? Did you create it yourself (perhaps via cut-and-paste), or did you download it from some site, or some other way? – John Gordon Commented Feb 18 at 2:33
  • 1 The character Ì (0xCC) does not exist in the data shown in the question. Your file was almost certainly created on a system (probably Windows) using cp1252 (ISO-8859-1) encoding. If you specify the correct encoding when opening the file you'll have more success. – Adon Bilivit Commented 2 days ago
  • Related: UnicodeDecodeError, invalid continuation byte – wjandrea Commented 2 days ago
Add a comment  | 

2 Answers 2

Reset to default 0

The code is correct but the problem is with the file's encoding.Try converting the file to utf-8 online or use python chardet to automatically detect the right encoding for your json file

import chardet
rawdata = open("test11.json", 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']

with open("test11.json","r", encoding=charenc) as file:
    data = json.load(file)
    print(data)

See about encoding from this other solution on StackOverflow here

The original file was create using textedit on my mac.

I opened dreamweaver and created a new json file and inserted my data.

I then tried my original code and it worked!!

发布评论

评论列表(0)

  1. 暂无评论