当前位置: 首页 > 知识库问答 >
问题:

在python中针对json模式验证tsv

江新
2023-03-14

我试图根据Python中的JSON模式验证TSV文件的行。下面是该模式的一个示例:

{
  "title": "employee",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "surname": {
      "type": "string"
    },
    "position": {
      "type": "string"
    },
    "telephone": {
      "type": "integer"
    },
  "required": [
    "name",
    "surname",
    "position",
    "telephone"
  ]
}

TSV文件如下所示:

NAME\tSURNAME\tPOSITION\tTELEPHONE
JOHN\tJOHNSON\tSECRETARY\t11111
    null

共有1个答案

郭炳
2023-03-14

您可以使用一些pandas和JSON进行自己的验证。请注意,您的模式示例无效,因为您有一个打开的花括号。我假设您打算在required标记之前关闭properties。下面的示例使用了以下内容:

{
  "title": "employee",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "surname": {
      "type": "string"
    },
    "position": {
      "type": "integer"
    },
    "telephone": {
      "type": "integer"
    }
  },
  "required": [
    "name",
    "surname",
    "position",
    "telephone"
  ]
}

注意,我还将position的数据类型更改为integer,作为一个不验证的示例。json模式存储在名为input.json的文件中,表格数据存储在名为input.tsv的文件中:

name  surname   position  telephone
John  Johnson  SECRETORY     111111

首先,我们导入模块并读取数据,如下所示:

import pandas as pd
import json

# Read tabular data
df = pd.read_csv("input.tsv", sep="\t")

# Read JSON data
with open("input.json") as json_file:
    data = json.load(json_file)
# Check that all required columns are present
required = data["required"]
cols_ok = all([col in df.columns for col in required])  # True
print("All required columns present: %s" % (cols_ok))
# Restructure the data types of our tabular data into a separate dataframe
dtypes = df.dtypes.reset_index()
dtypes.columns = ["column", "data_type"]
# There are discrepancies in naming conventions between your JSON file
# and how pandas reports data types, so we need to change them to a common format
dtypes.data_type = dtypes.data_type.replace("object", "string")  # Pandas calls it "object", you call it "string"
dtypes.data_type = dtypes.data_type.replace("int64", "integer")  # Pandas calls it "int64", you call it "integer"

# Extract the column names and the expected data type for that column
for column_name, subdict in data["properties"].items():
    expected_dtype = subdict["type"]
    actual_dtype = dtypes[dtypes.column == column_name].data_type.iloc[0]
    # Check if it matches our actual data types
    if expected_dtype == actual_dtype:
        print("Correct data type for column '%s' (%s)" % (column_name, expected_dtype))
    else:
        print("WRONG data type for column '%s' (expected '%s', found '%s')" % (column_name, expected_dtype, actual_dtype))

输出:

Correct data type for column 'name' (string)
Correct data type for column 'surname' (string)
WRONG data type for column 'position' (expected 'integer', found 'string')
Correct data type for column 'telephone' (integer)
 类似资料:
  • 在C#中是否有任何验证器可以使用,即使没有指令,也会使额外的属性失败?

  • 问题内容: 我有一个XML文件,并且有一个XML模式。我想针对该架构验证文件,并检查其是否符合该架构。我正在使用python,但是如果python中没有这样有用的库,则可以使用任何语言。 我在这里最好的选择是什么?我会担心如何快速启动和运行它。 问题答案: 绝对可以。 使用预定义的架构定义,加载文件并捕获任何XML架构错误: 关于编码的注意事项 如果模式文件包含带有编码(例如)的xml标记,则上面

  • 我有一个用例,我将把一个json-schema作为输入,验证它,然后保存在我的系统中。稍后,我将获取json数据,我需要使用上面提到的json-schema来验证这些数据。给定这个场景,我需要执行两个级别的验证: 我使用的是json-schema-validator jar,只能找到第二级验证,在文档中找不到json-schema验证。例如:假设我们有以下示例json-schema:

  • 问题内容: 我有工作,并为我解编和编组XML转换为Java对象,反之亦然。现在,我试图根据我们的模式(test.xsd)来验证我们的XML。假设我的XML中缺少任何必填字段,那么我想根据模式test.xsd验证XML后知道哪个字段缺失。 如何根据test.xsd模式验证XML。我的test.xsd模式路径是- C:\ workspace \一\二\三\ src \ main \ java \ co

  • 我试图使用另一个JSON模式来验证JSON模式。 要验证的JSON模式示例:https://jsonschema.net/home 验证上述模式的验证模式参考:https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json 从上面的中,如果我删除,我的JSON将变得无效。 我有什么方法可以定义根

  • 我在验证JSON时遇到了一些错误。我无法理解这些错误,有人能帮我解释一下吗。 } 这是json。 消息:JSON与“anyOf”中的任何模式都不匹配。架构路径: 消息:无效类型。应为数组,但得到的是字符串。架构路径: 消息:值“标识”载体“未在枚举中定义。架构路径: 消息:字符串“uui # abb 0 ef 56-8562-4056-aa62-AFB 758 a 150 ad”未根据格式“uri