Python JSON输出将所有行放在一行中

Python JSON输出将所有行放在一行中,python,json,python-3.x,converters,Python,Json,Python 3.x,Converters,我想将文本文件转换为JSON。我遇到的问题是,它以某种方式将txt文件中的所有行添加到JSON中的一个很长的行中。请帮忙 import re import os import json # list of files to be sent to Json file loadfile = open("aivc-tests/baseline/clf_risk_start.txt", "r") print("Loading file..."

我想将文本文件转换为JSON。我遇到的问题是,它以某种方式将txt文件中的所有行添加到JSON中的一个很长的行中。请帮忙

import re
import os
import json

# list of files to be sent to Json file
loadfile = open("aivc-tests/baseline/clf_risk_start.txt", "r")
print("Loading file..."
#takes away the extention from name of file
name_of_file = "clf_risk_start"
print("Removing unnecessary items...")
if loadfile.mode == 'r':
    contents = loadfile.read()
    #remove all unnecessary items
    remove_dashes = re.sub("-","", contents)
    remove_hashes =re.sub("##", "", remove_dashes)
    remove_intent =re.sub("intent", "", remove_hashes)
    remove_colan =re.sub(":", "", remove_intent)
    remove_generic =re.sub("Generic", "", remove_colan)
    remove_critical =re.sub("critical", "", remove_generic)
    remove_line_one=re.sub("<! Generated using Chatette v1.6.2 >", "", remove_critical)
    new_line_removed =remove_line_one.strip().replace('\n', ',')
    edited_contents = new_line_removed  
    # print(edited_contents)
    #return(edited_contents)
print("Formating...")
data1 = {}
data1['clf_test_utterances'] = []
data1['clf_test_utterances'].append({
    name_of_file: edited_contents
 })
data2 = {}
data2['testing Suit'] = []
data2['testing Suit'].append({
    'Name': 'test',
    'Description': 'this is just a test',
    'Test Dialogue': '',
    "format_version": 5,
    "clf_test_utterances": data1
})
print("Exporting to json")
# this will write to json file
with open('test_suit_single.json', 'w') as outfile:
    json.dump(data2, outfile)
print('successfully converted')
我怎么能把它们都分门别类呢?

就是这样。它不会基于宽度分割文本,因为一个。(换行符将始终转义。)

您应该在文本编辑器中启用换行。如果确实要将行拆分为多行,可以使用数组

替换:

new\u line\u removed=删除一个.strip()。替换('\n',',')

与:


new\u-line\u-removed=remove\u-line\u-one.strip().split('\n')

为什么会有这个问题?所以用一个
indent=4
参数转储它实际上,看着输出,我不明白这个问题,如果你想在行之间拆分它(在JSON中),考虑使用一行行,而不是并列成一个STR.替换代码< NexyLynRead=RevivyLythION.Load()。并且您从
编辑的内容中删除了所有换行符。那么,你认为换行符是从哪里来的呢?
{
   "testing Suit":[
      {
         "Name":"test",
         "Description":"this is just a test",
         "Test Dialogue":"",
         "format_version":5,
         "clf_test_utterances":{
            "clf_test_utterances":[
               {
                  "clf_risk_start":"I am experiencing signs of covid 19. How can I get tested to understand if I i am well as i think?, I am experiencing symptoms of coronvirus. How can I be checked out to verify if I have it or not?, I am feeling signs of corona. How can I be tested to be sure if I I should self isolate?, I am feeling signs of coronvirus. How can I test to learn if I i am healthy?, I am ms of covid 19. How can I "
               }
            ]
         }
      }
   ]
}