简体   繁体   English

从 Excel 生成 JSON 文件以在 Python 中读取

[英]Generating a JSON file from Excel to be read it in Python

I have a quite big Excel file containing several tables.我有一个相当大的 Excel 文件,其中包含几个表。
I was able to serialize them into JSONs using an ADODB.Stream in VBA code:我能够使用 VBA 代码中的 ADODB.Stream 将它们序列化为 JSON:

This is VBA code:这是 VBA 代码:

Dim st As ADODB.Stream
  ' create a stream object
Set st = New ADODB.Stream
' set properties
st.Charset = "utf-8"
st.Type = adTypeText
' open the stream object and write some text
st.Open
st.WriteText myString
st.SaveToFile filepath, adSaveCreateOverWrite
st.Close

Now I want to read it in Python to pass it to a data frame with:现在我想在 Python 中读取它以将其传递给具有以下内容的数据帧:

This is Python code:这是 Python 代码:

import JSON
with open(myfilecomplete) as f:
    data = json.load(f)

I get this error:我收到此错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 407330: character maps to UnicodeDecodeError:“charmap”编解码器无法解码 position 中的字节 0x9d 407330:字符映射到

The file is quite big (1MB of text).该文件很大(1MB 的文本)。 I dont even know how to find character 407330.我什至不知道如何找到字符 407330。

Besides I am supposed to be writting in disc in uft-8 the most common encoding form.此外,我应该以最常见的编码形式以 uft-8 写入光盘。 Right?正确的?

Why is JSON not able to undecode utf-8?为什么 JSON 无法解码 utf-8?

EDIT AFTER ANSWER / WARNING: Don't try to serialise a JSON file from Excel manually.回答/警告后编辑:不要尝试手动从 Excel 序列化 JSON 文件。 this will end up giving errors almost always.这几乎总是会导致错误。 Use a proper library for doing that like in here :使用适当的库来执行此操作,如下所示

You get it:你懂了:


with open(myfilecomplete, encoding='utf-8-sig') as f:
    data = json.load(f)

Here some documentation 这里有一些文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM