简体   繁体   English

json值仅写入python中的第一个字段

[英]json values only writing to first field in python

I'm trying to write csv data (MAC addresses and signal strength values) to a JSON file using Python, but the data is only writing to the first field. 我正在尝试使用Python将csv数据(MAC地址和信号强度值)写入JSON文件,但是数据仅写入第一个字段。 How do I get each signal strength value to write to the correct MAC address? 如何获得每个信号强度值以写入正确的MAC地址?

This is my csv data (total of 30 addresses): 这是我的csv数据(总共30个地址):

    wifi                  rss
    b0:7f:b9:bc:f0:e2   -56
    34:a8:4e:fc:13:50   -59
    34:a8:4e:fd:3c:50   -57
    34:a8:4e:fd:3c:53   -55
    34:a8:4e:fc:13:53   -58
    b0:7f:b9:bc:f1:02   -81
    b0:7f:b9:bc:f0:e0   -55
    ec:58:ea:59:a3:53   -89
    b0:7f:b9:bc:f0:40   -69
    b0:7f:b9:bc:f0:42   -67
    00:1d:7e:42:e0:ba   -60
    b0:7f:b9:bc:f0:f0   -63
    b0:7f:b9:bc:f0:50   -72
    b0:7f:b9:bc:f0:f2   -64
    0a:8d:cb:65:6d:70   -85
    06:8d:cb:65:6d:70   -85

This is my code so far 到目前为止,这是我的代码

import csv
import json
import pandas as pd

my_csv = pd.read_csv('my_data.csv')
fo = open('my_json.json','w')

wifi = my_csv.wifi

f = pd.read_csv('my_data.csv',usecols=[2])
rss = f.rss

fieldnames = (wifi)
reader = csv.DictReader( f, wifi)
for row in reader:
    json.dump(row, fo, indent = 2)
    fo.write('\n')

this is what I'm getting 这就是我得到的

"b0:7f:b9:bc:f0:e2": "rss",
"34:a8:4e:fc:13:50": null,
"34:a8:4e:fd:3c:50": null,
"34:a8:4e:fd:3c:53": null,
"34:a8:4e:fc:13:53": null,
"b0:7f:b9:bc:f1:02": null,
"b0:7f:b9:bc:f0:e0": null,
"ec:58:ea:59:a3:53": null,
"b0:7f:b9:bc:f0:40": null,
"b0:7f:b9:bc:f0:42": null,
"00:1d:7e:42:e0:ba": null,
"b0:7f:b9:bc:f0:f0": null,
"b0:7f:b9:bc:f0:50": null,
"b0:7f:b9:bc:f0:f2": null,
"0a:8d:cb:65:6d:70": null,

You are dumping inserting data to json at starting row. 您正在将插入数据转储到起始行的json中。 Thus it is printing same. 因此它正在打印相同。 You need to append Data. 您需要附加数据。 You can append data to any dictionary and dump that complete dictionary to Json 您可以将数据追加到任何字典,然后将完整的字典转储到Json

    # Out of Loop
    data = {}

    #Inside Loop
    data.append("What ever Json You want to insert")

    #Outside Loop
    json.dump(data, fo,[other arguments])
with open(file_name, 'w') as jsonfile:
    json.dump(dictionary, jsonfile, indent=4)

Note that this should be used after you have created a dictionary from wifi and rss. 请注意,从wifi和rss创建dictionary后,应使用此功能。

This is one approach. 这是一种方法。

Demo: 演示:

import json
import pandas as pd

my_csv = pd.read_csv('my_data.csv', sep="\s*")
result = {"wifi": {k:v for k, v in my_csv.to_dict('split')["data"]}}

with open('my_json.json', 'w') as outfile:
    json.dump(result, outfile)

since you are using pandas you can use to_json method described here 由于您使用的是熊猫,因此可以使用to_json介绍的to_json方法

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html

I think for this case the following code should do the trick. 我认为在这种情况下,以下代码可以解决问题。

import pandas as pd

dataFrame = pd.read_csv('my_data.csv')
dataFrame.set_index('wifi').to_json('path_of.json')

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

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