在Python中将字符串转换为int的值错误

时间:2015-10-11 15:04:54

标签: python

我正在尝试从列表中创建一个带有表示日期的字符串的程序,并将其与另外两个日期进行比较。如果它在这两个日期之间,程序会将列表中的字符串替换为分开的日期,月份和年份。

问题是,如果我将该字符串转换为'int',在控制台中它没关系,但是当我运行程序时出现以下错误:

$subreddit = Subreddit::with('posts.votes')->with('moderators.user')->where('id', $subreddit->id)->first();
$posts = $subreddit->posts()->paginate(2);

那是我的代码:

<link rel="stylesheet" href="{{ URL::asset('assets/css/jquery.upvote.css') }}">
    <script src="{{ URL::asset('assets/js/jquery.upvote.js') }}"></script>
    <script type="text/javascript" src="{{ asset('assets/js/jquery.jscroll.min.js') }}"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('.topic').upvote();

            $('.vote').on('click', function (e) {
                e.preventDefault();
                var $button = $(this);
                var postId = $button.data('post-id');
                var value = $button.data('value');
                $.post('http://localhost/reddit/public/votes', {postId:postId, value:value}, function(data) {
                    if (data.status == 'success')
                    {
                        // Do something if you want..
                    }
                }, 'json');
            });

            $('.scroll').jscroll({
                autoTrigger: true,
                nextSelector: '.pagination li.active + li a',
                contentSelector: 'div.scroll',
                callback: function() {
                    $('ul.pagination:visible:first').hide();
                }
            });
        });
    </script>

<div class="scroll">
   @foreach($posts as $post)
        @include('partials/post')
   @endforeach
   {!! $posts->render() !!}
</div>

所以,

ValueError: invalid literal for int() with base 10: ''

应该返回def date(lst, d1, d2): for d in lst: if int(d[4:])>=int(d1[4:]) or int(d[4:])<=int(d2[4:]): if int(d[2:4])>=int(d1[2:4]) or int(d[2:4])<=int(d2[2:4]): if int(d[0:2])>=int(d1[0:2]) or int(d[0:2])<=int(d2[0:2]): j=d j1=j[0:2] j2=j[2:4] j3=j[4:] lst.insert(lst.index(d),j3) lst.insert(lst.index(d),j2) lst.insert(lst.index(d),j1) lst.remove(d) return lst

1 个答案:

答案 0 :(得分:0)

注释掉这两行,它会起作用:

lst.insert(lst.index(d), j2)

lst.insert(lst.index(d), j1)

显然,当你循环浏览列表时,同样如此 时间删除,并在开始索引中插入错误。

为避免麻烦,请使用列表副本:

def date(lst, d1, d2):
    lstC = list(lst)
    for d,dd in  zip(lst, lstC):
        if int(d[4:]) >= int(d1[4:]) or int(d[4:]) <= int(d2[4:]):
            if int(d[2:4]) >= int(d1[2:4]) or int(d[2:4]) <= int(d2[2:4]):
                if int(d[0:2]) >= int(d1[0:2]) or int(d[0:2]) <= int(d2[0:2]):
                    j = d
                    j1 = j[0:2]
                    j2 = j[2:4]
                    j3 = j[4:]
                    ind_d = lst.index(d)
                    lstC.insert(ind_d, j3)
                    lstC.insert(ind_d, j2)
                    lstC.insert(ind_d, j1)
                    lstC.remove(dd)

    return lstC

print(date(['24012014', '22032015', '03022015', '15122014', '11112015'], '22022014', '10112015'))

['24', '22', '02', '2015', '03', '2015', '2014', '22032015', '03022015', '15122014', '11112015']

如果从头开始循环,则可以避免索引错误。但是你需要 显而易见地改变你的脚本的逻辑。

def date(lst, d1, d2):
    # from copy import deepcopy
    for i in range(len(lst) - 1, -1, -1):
        if int(lst[i][4:])>=int(d1[4:]) or int(lst[i][4:])<=int(d2[4:]):
            if int(lst[i][2:4])>=int(d1[2:4]) or int(lst[i][2:4])<=int(d2[2:4]):
                if int(lst[i][0:2])>=int(d1[0:2]) or int(lst[i][0:2])<=int(d2[0:2]):
                    j=lst[i]
                    j1=j[0:2]
                    j2=j[2:4]
                    j3=j[4:]
                    ind_d = lst.index(lst[i])
                    lst.insert(ind_d,j3)
                    lst.insert(ind_d,j2)
                    lst.insert(ind_d,j1)
                    lst.remove(lst[i])

    return lst

print(date(['24012014', '22032015', '03022015', '15122014', '11112015'], '22022014', '10112015'))

['01', '2014', '24012014', '03', '2015', '22032015', '02', '2015', '03022015', '15122014', '11112015']