将列表从str类型转换为类型列表

时间:2017-02-05 16:28:53

标签: python

一定很简单,但我不知道:

我有一个带有此内容的 file.json

<code>
//my JAVASCRIPT...
$(document).ready(function(){

//sub button functionality...
$("#sub").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"userinfo.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            $("#sub_msg").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});


            }
        })
    })



$("#sub2").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"userinfo2.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            $("#sub_msg").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});


            }
        })
    })

$("#sub3").click(function(event){
    //alert(1);
    event.preventDefault();
    $.ajax({
            url:"userinfo3.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            $("#sub_msg").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});


            }
        })
    })

//show data numbers only
//number of customers in ticket
setInterval(function(){$('#customerin').load('customerin.php')},100);
setInterval(function(){$('#customerin2').load('customerin2.php')},100);
setInterval(function(){$('#customerin3').load('customerin3.php')},100);

//number of customers in queue
setInterval(function(){$('#customerout').load('customerout.php')},100);
setInterval(function(){$('#customerout2').load('customerout2.php')},100);
setInterval(function(){$('#customerout3').load('customerout3.php')},100);

//shows list of cardnumbers that is in queue FIFO
setInterval(function(){$('#customers').load('ticketin.php')},100);
setInterval(function(){$('#customers2').load('ticketin2.php')},100);
setInterval(function(){$('#customers3').load('ticketin3.php')},100);

//shows tables of each customer
setInterval(function(){$('#tabs').load('calling.php')},100);
setInterval(function(){$('#tabs2').load('calling2.php')},100);
setInterval(function(){$('#tabs3').load('calling3.php')},100);

//Format all tables and reseting the autoincrement counters...
$("#purge").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"purge.php",
            method:"POST",
            success:function(data){
            //aler('message recieved');
            $("#sub_msg").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#call1").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"call1.php",
            method:"POST",
            success:function(data){
            //aler('message recieved');
            $("#product_msg1").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#call1").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"play.php",
            method:"POST",
            success:function(data){
            $("#embed").alert('message recieved');
            //$("#product_msg1").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#call1").click(function(event){
    <audio id='audiotag1' src='sound/bell.mp3' preload='auto' autoplay></audio>

    })

$("#call2").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"call2.php",
            method:"POST",
            success:function(data){
            //aler('message recieved');
            $("#product_msg2").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#call3").click(function(event){
    //alert(0);
    event.preventDefault();
    $.ajax({
            url:"call3.php",
            method:"POST",
            success:function(data){
            //aler('message recieved');
            $("#product_msg3").html(data);
            $('#embed').load('play.php').html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#recall").click(function(event){
    //alert(0);
    event.preventDefault();
        $.ajax({
            url:"recall.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            //aler('message recieved');
            $("#product_msg1").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#recall2").click(function(event){
    //alert(0);
    event.preventDefault();
        $.ajax({
            url:"recall2.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            //aler('message recieved');
            $("#product_msg2").html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

$("#recall3").click(function(event){
    //alert(0);
    event.preventDefault();
        $.ajax({
            url:"recall3.php",
            method:"POST",
            data:$("form").serialize(), 
            success:function(data){
            //aler('message recieved');
            $("#product_msg3").html(data);
            $('#embed').load('play.php').html(data);
            //.fadeTo(1000, 500).slideUp(500, function(){$("#sub_msg").alert('close');});
            }
        })
    })

})
</code>

<code>
//my display page:
<div class="container-fluid">

            <!--navbar brand-->
            <div class="navbar-header">
            <a href="#" class="navbar-brand">LOGO</a>
            </div>
        </div>
    </div>
    <!--navbar end-->

    <p></br></p>
    <p></br></p>
    <p></br></p>

<!--content page start-->
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12 col-xs-12">

                <div class="row">
                    <div class="col-md-12 col-xs-12" id="product_msg">
                    </div>
                    <div id="embed"></div>

                </div>

                <!--start panel-->
                <div class="panel panel-info" id="scroll">
                    <div class="panel-heading" style="text-align:center;font-size:90px;font-weight:bold;">NOW SERVING</div>
                    <div class="panel-body">

    <!--COUNTER 1-->
    <div class="bs-calltoaction bs-calltoaction-default">
        <div class="row">
            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;padding-top: 34px;">
                COUNTER #1  
            </div>

            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;">
                <div class="well well-lg" style="margin-bottom: 0px;">
                    <div id="tabs"></div> 
                </div>  
            </div>
        </div>       
    </div>

    <!--COUNTER 2-->
    <div class="bs-calltoaction bs-calltoaction-default">
        <div class="row">
            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;padding-top: 34px;">
                COUNTER #2  
            </div>

            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;">
                <div class="well well-lg" style="margin-bottom: 0px;">
                    <div id="tabs2"></div>  
                </div>  
            </div>
        </div>       
    </div>

    <!--COUNTER 3-->
    <div class="bs-calltoaction bs-calltoaction-default">
        <div class="row">
            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;padding-top: 34px;">
                COUNTER #3  
            </div>

            <div class="col-md-6" style="text-align:center;font-size:70px;font-weight:bold;">
                <div class="well well-lg" style="margin-bottom: 0px;">
                    <div id="tabs3"></div>  
                </div>  
            </div>
        </div>       
    </div>

                        </div>

                    <div class="panel-footer">&copy; 2017 Created by W.G.D.A.</div><!--footer for col-md-8-->

                    <!--
                    <div class="marquee">
                    <marquee behavior=scroll direction="left" scrollamount="15">Your message here</marquee>
                    </div>-->

                </div>
                <!--end panel-->
            </div>
        </div>
    </div>
<!--content page end-->
</code>

<code>
//the counter page
<!--content page start-->
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2"></div><!--space-->

            <div class="col-md-8 col-xs-12">

                <div class="row">
                    <div class="col-md-8 col-xs-12" id="product_msg1">
                        <!--MESSAGE HERE-->
                    </div>
                </div>

            <div class="panel panel-info" id="scroll">
                <div class="panel-heading"><h1>COUNTER #1</h1></div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <!--1-->
                                <div class="panel panel-info" id="scroll">
                                    <div class="panel-heading"><h3>NOW SERVING</h3></div>
                                        <div class="panel-body">
                                            <div id="tabs" style="text-align:center;font-size:70px;font-weight:bold;"></div>
                                        </div>
                                </div>
                                <!--2-->
                                <div class="panel panel-info" id="scroll">
                                    <div class="panel-heading"><h3>NEXT QUEUE</h3></div>
                                        <div class="panel-body">
                                            <div id="customers"></div>
                                        </div>
                                </div>
                                <!--3-->
                                <div style="text-align:center;margin-bottom:20px;">
                                    <input style="margin-right:5px;" value="CALL" type="button" id="call1" name="signup_button" class="btn btn-success">
                                    <INPUT style="" TYPE="button" onClick="history.go(0)" VALUE="Refresh" class="btn btn-default">

                                </div>  

                            </div>
                            <div class="col-md-6">
                                <div class="panel panel-info" id="scroll">
                                    <div class="panel-heading"><h3>MISSED QUEUE</h3></div>
                                        <div class="panel-body">
                                        <form id="myForm" action="recall.php" method="post">
                                            <div class="form-group updown">
                                                <input type="number" name="renumber" id="renumber" class="form-control textboxsize1" required style="margin:auto;"  size="3" autocomplete="off" min="1" max="999" onkeypress="return isNumeric(event)" oninput="maxLengthCheck(this)" tabindex="1" value="0" data-toggle="modal" data-target="#myModal">
                                            </div>
                                            <div style="text-align:center;">
                                                <input style="margin-right:5px;" value="RECALL" type="button" id="recall" name="recall" class="btn btn-success">
                                            </div>
                                        </form> 
                                        </div>
                                </div>

                                <div style="text-align:center;">
                                    <a href="page2.php" class="btn btn-default">NEXT PAGE</a>
                                </div>
                            </div>

                        </div>
                    </div>
                <div class="panel-footer">&copy; 2017 Created by W.G.D.A.</div><!--footer for col-md-8-->
            </div>

            </div>

            <div class="col-md-2"></div><!--space-->        
        </div>
    </div>
<!--content page end-->
</code>

并将其导入为[{u'key': u'value1'},{u'key': u'value2'}]

data

我很高兴这很有效

with open(file.json) as fd:
        data = fd.read()

但遗憾的是,>>> print (data) [{u'key': u'value1'},{u'key': u'value2'}] 现已转换为data

str

所以我不能使用>>> print(type(data)) <type 'str'> 来删除项目。

如何将data转换为正常的词典列表? (data

2 个答案:

答案 0 :(得分:3)

如果使用文件方法.read(),则总是会得到一个字符串。您必须自己解析它,但幸运的是,Python有一个json module可以为您完成所有解析。它甚至可以读取文件:

import json
with open(file.json) as fd:
    data = json.load(fd)

但是,正如Stefan指出的那样,你的文件不是有效的JSON - 它看起来像是Python对象的repr()输出。在这种情况下,它几乎一样简单:

import ast
with open(file.json) as fd:
    data = ast.literal_eval(fd.read())

答案 1 :(得分:1)

使用json库非常简单。

import json
obj = json.loads(json_string) 

将json_string(json_string是包含JSON文档的str或unicode实例)反序列化为Python对象(在我们的示例中为dixt)。

修改

正如Stefan所说,这不是一个有效的json(它看起来像一个列表),因此你可以看到Tim的答案。