如何将每个文本文件行分成单独的列表?

时间:2016-02-19 11:51:37

标签: python list

说我有一个包含以下内容的文本文件:

Bob 15 M
John 16 M
Jen 15 F

如何将其放入列表中,以便列表值为:

listOne = ['Bob', '15', 'M']
listTwo = ['John', '16', 'M']
listThree = ['Jen', '15', 'F']

由于

4 个答案:

答案 0 :(得分:4)

将它拆分并解压缩

    <script type="text/javascript">

    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "Index.aspx/GetCountry",
            data: '',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    $("#ddlCountry").append($("<option></option>").val(data.d[i].Id).html(data.d[i].Name));
                }

                //for multiselect
                $('#ddlCountry').multiselect({
                    includeSelectAllOption: true
                });
                $.ajax({
                    type: "POST",
                    url: "Index.aspx/GetSelctedCountry",
                    data: '',
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var valArr = data.d.split(',');
                        i = 0, size = valArr.length;
                        //  //for multiselect  show the selcted country as selected if
                        for (var i = 0; i < data.d.length; i++) {
                            $("#ddlCountry").find(":checkbox[value='" + valArr[i] + "']").attr("checked", "checked");
                            $("#ddlCountry option[value='" + valArr[i] + "']").attr("selected", 1).change();
                            $("#ddlCountry").multiselect("refresh");
                        }
                    },
                    error: function (err) {

                    }
                });
            },
            error: function (err) {

            }
        });
        var obj = {};
        obj.state = '';
        $.ajax({
            type: "POST",
            url: "Index.aspx/GetState",
            data: JSON.stringify(obj),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    $("#ddlState").append($("<option></option>").val(data.d[i].Id).html(data.d[i].Name));
                }
                var obj = $("#MainContent_ChildContent2_ddlCountryHid").val();
                //for multiselect
                $('#ddlState').multiselect({
                    includeSelectAllOption: true,
                    height: 50,
                });

                $('#ddlState').multiselect("disable");
            },
            error: function (err) {

            }
        });
        $('#ddlState').change(function () {
            var d1 = '';
            $('#ddlState :checked').each(function (i) {
                d1 = d1 + $(this).val() + ',';
            });
            $("#MainContent_ChildContent2_ddlStateHid").val(d1);
        });
        $('#ddlCountry').change(function () {
            var d = '';
            $('#ddlCountry :checked').each(function (i) {
                d = d + $(this).val() + ',';
            });
            $("#MainContent_ChildContent2_ddlCountryHid").val(d);
            var obj = {};
            obj.state = d;
            $.ajax({
                type: "POST",
                url: "Index.aspx/GetState",
                data: JSON.stringify(obj),
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var data1 = '';
                    $('#ddlState option').each(function (index, option) {
                        if (data1 == '')
                            data1 = $(option);
                        $(option).remove();
                    });
                    //for multiselect
                    $('#ddlState').multiselect({
                        includeSelectAllOption: true
                    });
                    if (data.d.length == 0) {
                        $("#ddlState").append($("<option></option>").val('').html('')).multiselect("destroy").multiselect();
                        $("#ddlState").attr('disable', 'disable');
                    }


                    for (var i = 0; i < data.d.length; i++) {
                        $("#ddlState").append($("<option></option>").val(data.d[i].Id).html(data.d[i].Name));
                    }
                    $.ajax({
                        type: "POST",
                        url: "Index.aspx/GetSelectedState",
                        data: '',
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var valArr = data.d.split(',');
                            i = 0, size = valArr.length;
                            //for multiselect show by default selected
                            for (i; i < size; i++) {
                                $("#ddlState").find(":checkbox[value='" + valArr[i] + "']").attr("checked", "checked");
                                $("#ddlState option[value='" + valArr[i] + "']").attr("selected", 1);
                                $("#ddlState").multiselect("refresh");
                            }
                        },
                        error: function (err) {

                        }
                    });

                    $('#ddlState').multiselect('rebuild');
                    $('#ddlState').multiselect({
                        includeSelectAllOption: true
                    });
                    if (data.d.length == 0)
                        $('#ddlState').multiselect("disable");
                    else
                        $('#ddlState').multiselect("enable");

                },
                error: function (err) {
                }
            });
        });
        $('#testid').click(function () {
            $('#ddlState option').prop('selected', true);
        });
    });


</script>

答案 1 :(得分:0)

使用open file和split():

with open('text.txt') as f:
    strings = f.readlines()
    data = [string.split() for string in strings]

答案 2 :(得分:0)

您可以将string.split应用于文件中的每一行:

import string

with open('text.txt') as text:
    result = map(string.split, text)

使用Python 3,您需要实现map,否则文件在关闭之前无法读取:

    result = list(map(string.split, text))

但是,string.split已弃用,仅适用于str个对象,而不适用于unicode。更通用的解决方案是创建列表解析:

    result = [line.split() for line in text]

答案 3 :(得分:-1)

with open('text.txt') as file:
    lines = file.readlines()        # Creates a list of lines
    for line in lines:
        line = line.split()         # Split each line into list of "words"

这为您提供了一个可能合理的嵌套列表。如果你拼命想要3个列表,请使用:

evalvar = ""
count = 0
for line in lines:
    count += 1
    evalvar += "list"+str(count)+" = "+str(line)+";"
exec(evalvar)                                         # Runs the evalvar which has
                                                      # code in it.