Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将json转换为csv文件时出错_Python - Fatal编程技术网

Python 将json转换为csv文件时出错

Python 将json转换为csv文件时出错,python,Python,现有的其他解决方案对我不起作用。我想将csv文件与json文件进行比较,以查看json文件as是否包含csv文件中的任何字符串 我试过(改编自其他stackoverflow帖子) 但它不起作用。我是否最好换一种方式,将csv转换成json 编辑 Json文件: {"tag":["security architecture","systems security engineering","architecture","program protection planning (ppp)","syst

现有的其他解决方案对我不起作用。我想将csv文件与json文件进行比较,以查看json文件as是否包含csv文件中的任何字符串

我试过(改编自其他stackoverflow帖子)

但它不起作用。我是否最好换一种方式,将csv转换成json

编辑

Json文件:

{"tag":["security architecture","systems security engineering","architecture","program protection planning (ppp)","system security engineering","security engineering"],"newtag":["security","architecture engineering & policy","certified ethical hacker","security policy and risk management","sse","enterprise transition plan","plan","tax","capacity analysis"]}
csv:


我想看看json文件与哪个id最匹配(因此我想知道每个id有多少个标记匹配),但我不确定如何处理json与csv的比较,我可能误解了您的问题,但希望这至少能让您开始

我相信一定有更好的方法,但这里有一个方法

首先,加载数据,将csv数据放入嵌套列表,将json数据放入dict。然后获取csv文件中的所有唯一ID

检查每个唯一ID的csv文件,并计算json标记中存在的标记数

# load csv data
with open("csvdata.csv") as csvFile:
    reader = csv.reader(csvFile)
    loadedCSV = [row for row in reader]

# load json data and get list of tags
jsonTags = json.load("jsonFile.json")["tags"]

# create a unique list of ids from csv file
uniqueIDs = list(set([row[0] for row in loadedCSV]]))

# best match so far
selectedID = None

# keep track of best count
maxCount = 0

# go through ids
for id in uniqueIDs:

    # count for specific ID
    idCount = 0

    # go through tags in csv and add one to count if in json tags
    for row in loadedCSV:
        if row[0] == id:
            if row[1] in jsonTags:
                idCount += 1
    # compare count to current max
    if idCount > maxCount:
        selectedID = id
如果计数大于当前最大值,则将该ID存储为最佳ID

循环完成后,您应该拥有json标记中标记最多的ID

# load csv data
with open("csvdata.csv") as csvFile:
    reader = csv.reader(csvFile)
    loadedCSV = [row for row in reader]

# load json data and get list of tags
jsonTags = json.load("jsonFile.json")["tags"]

# create a unique list of ids from csv file
uniqueIDs = list(set([row[0] for row in loadedCSV]]))

# best match so far
selectedID = None

# keep track of best count
maxCount = 0

# go through ids
for id in uniqueIDs:

    # count for specific ID
    idCount = 0

    # go through tags in csv and add one to count if in json tags
    for row in loadedCSV:
        if row[0] == id:
            if row[1] in jsonTags:
                idCount += 1
    # compare count to current max
    if idCount > maxCount:
        selectedID = id

定义“它不工作”我们是否有可能看到json和csv文件的格式?没有进行比较,您只是在写入文件。到底应该比较什么?添加了json和csv。我已经编写了python代码来比较它是否是两个csv。但我有多个json文件,我希望能够运行。因此,手动创建csv文件效率低下。您的代码片段与您的问题究竟有何关联?谢谢RichSmith,我将对此进行研究。我有一些类似的代码,但在处理json时遇到了麻烦。当我打印selectedID或maxCount时,虽然没有显示任何内容。它似乎在idCount=+1之后中断。这是因为它与csv中的id而不是标记进行比较吗?@karmesto在循环之前打印UniqueID、loadedCSV和jsonTags可能是值得的,只是为了确保它们的格式正确并且加载正确,我会在上午检查其余的代码:)
# load csv data
with open("csvdata.csv") as csvFile:
    reader = csv.reader(csvFile)
    loadedCSV = [row for row in reader]

# load json data and get list of tags
jsonTags = json.load("jsonFile.json")["tags"]

# create a unique list of ids from csv file
uniqueIDs = list(set([row[0] for row in loadedCSV]]))

# best match so far
selectedID = None

# keep track of best count
maxCount = 0

# go through ids
for id in uniqueIDs:

    # count for specific ID
    idCount = 0

    # go through tags in csv and add one to count if in json tags
    for row in loadedCSV:
        if row[0] == id:
            if row[1] in jsonTags:
                idCount += 1
    # compare count to current max
    if idCount > maxCount:
        selectedID = id