使用python将CSV转换为JSON,并使用分隔符“ |”分割csv

时间:2019-03-06 06:51:18

标签: python json csv

schoolname|category|gender|medium_of_inst|address|area|pincode|landmark
----------+----------+----------+----------+----------+----------+------
qqq|qqq|qq|aa|asd|wer|asd|wert
www|fgh|qq|aa|sg|wer|asd|wert
eee|fxg|qq|aa|axcvsd|wer|asd|wert

如何删除第二行并拆分“ |”并将其转换为json

2 个答案:

答案 0 :(得分:0)

尝试以下代码。首先将字符串除以\n,然后删除第二行,然后除以|。希望这会有所帮助。

import json

strings = '''schoolname|category|gender|medium_of_inst|address|area|pincode|landmark
----------+----------+----------+----------+----------+----------+------
qqq|qqq|qq|aa|asd|wer|asd|wert
www|fgh|qq|aa|sg|wer|asd|wert
eee|fxg|qq|aa|axcvsd|wer|asd|wert'''

json_file_name = 'test.json'

strings = strings.split('\n') #Split the string by newline \n
del strings[0] #Remove the heading columns
del strings[0] #Remove the string starts with ----------+
data = []

try:
  for string in strings:
    row = string.split('|') #Split and write to json 
    row_data = {};
    row_data['schoolname'] = row[0]
    row_data['category'] = row[1]
    row_data['gender'] = row[2]
    row_data['medium_of_inst'] = row[3]
    row_data['address'] = row[4]
    row_data['area'] = row[5]
    row_data['pincode'] = row[6]
    row_data['landmark'] = row[7]
    data.append(row_data)

  with open(json_file_name, 'w') as outfile:
    json.dump(data, outfile)

  #Use the below to read the file  
  with open(json_file_name) as file_object:
    # store file data in object
    data = json.load(file_object)
    print(data)

except Exception as e:
      print("Type error: " + str(e)) 

输出

[
  {
    'schoolname': 'qqq',
    'category': 'qqq',
    'gender': 'qq',
    'medium_of_inst': 'aa',
    'address': 'asd',
    'area': 'wer',
    'pincode': 'asd',
    'landmark': 'wert'
  },
  {
    'schoolname': 'www',
    'category': 'fgh',
    'gender': 'qq',
    'medium_of_inst': 'aa',
    'address': 'sg',
    'area': 'wer',
    'pincode': 'asd',
    'landmark': 'wert'
  },
  {
    'schoolname': 'eee',
    'category': 'fxg',
    'gender': 'qq',
    'medium_of_inst': 'aa',
    'address': 'axcvsd',
    'area': 'wer',
    'pincode': 'asd',
    'landmark': 'wert'
  }
]

答案 1 :(得分:0)

以下是可帮助您的代码段:

import pandas as pd

df = pd.read_csv('test_old.csv',skiprows=2, 
  names=['schoolname', 'category', 'gender','medium_of_inst',
         'address','area','pincode','landmark'],   
       sep='|', engine='python')

json_output = df.to_json(orient='records')[1:-1].replace('},{', '} {')

print(json_output)

输出:

[    {       “ schoolname”:“ qqq”,       “类别”:“ qqq”,       “ gender”:“ qq”,       “ medium_of_inst”:“ aa”,       “ address”:“ asd”,       “ area”:“ wer”,       “ pincode”:“ asd”,       “ landmark”:“ wert”    },    {       “ schoolname”:“ www”,       “ category”:“ fgh”,       “ gender”:“ qq”,       “ medium_of_inst”:“ aa”,       “ address”:“ sg”,       “ area”:“ wer”,       “ pincode”:“ asd”,       “ landmark”:“ wert”    },    {       “ schoolname”:“ eee”,       “ category”:“ fxg”,       “ gender”:“ qq”,       “ medium_of_inst”:“ aa”,       “ address”:“ axcvsd”,       “ area”:“ wer”,       “ pincode”:“ asd”,       “ landmark”:“ wert”    } ]