Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: ValueError: invalid literal for int() with base 10:
Forum PHP.pl > Inne > Hydepark
Grzesiek1
Kawałek kodu skryptu:
Kod
def phpbb2_posts():
    '''Create table phpbb2_posts'''        
    mybb_posts = open_dump('posts')
    phpbb2_posts = list()
    enable_bbcode = 1
    enable_html = 0
    enable_smilies = 1
    enable_sig = 1
    post_edit_time = 'NULL'
    post_edit_count = 0
    #We need to get the ID of the newest topic
    newest_topic = 1
    for row in xrange(len(mybb_posts)):
        post_id = int(mybb_posts[row][0]) #pid
        topic_id = int(mybb_posts[row][1]) #tid
        forum_id = int(mybb_posts[row][3]) #fid
        poster_id = int(mybb_posts[row][6]) #uid
        post_time = int(mybb_posts[row][8]) #dateline
        poster_ip = mybb_posts[row][10].rstrip("'").lstrip("'") #ipaddress
        #poster_ip must be hex
        string_end = len(poster_ip)
        start = 0
        string_hex = ''
        for number_block in xrange(4):
            end = poster_ip.find('.', start, string_end)
            #Last number block of the ip address (XXX.XXX.XXX.XXX)
            if number_block == 3:
                end = string_end
            number = int(poster_ip[start:end])        //tutaj wypada 305 linja
            start = end + 1
            hexnumber = "%x" %number
            if len(hexnumber) == 1:
                hexnumber = '0' + hexnumber
            string_hex += str(hexnumber)
        poster_ip = "'" + string_hex + "'"
        post_username = mybb_posts[row][7] #username
        phpbb2_posts.append((post_id, topic_id, forum_id, poster_id,
                            post_time, poster_ip, post_username,
                            enable_bbcode, enable_html,enable_smilies,
                            enable_sig, post_edit_time, post_edit_count))
        if topic_id > newest_topic:
            newest_topic = topic_id
    columns_names =  ('post_id', 'topic_id', 'forum_id', 'poster_id',
            'post_time','poster_ip', 'post_username', 'enable_bbcode',
            'enable_html', 'enable_smilies', 'enable_sig', 'post_edit_time',
            'post_edit_count')
    #We need the the ID of the last post of each topic
    topic_last_post_id_list = list()
    #Now this is a bad solution, I guess
    #We create a list with newest_topic elements
    for i in xrange(newest_topic + 1):
            topic_last_post_id_list += ["''"]
    for row in xrange(len(phpbb2_posts)):  
        #phpbb2_posts[row][1] is the thread number
        #phpbb2_posts[row][0] is the post number
        topic_last_post_id_list[phpbb2_posts[row][1]] = phpbb2_posts[row][0]            
    return phpbb2_posts, columns_names, topic_last_post_id_list


Błąd(zaznaczylem wam w kodzie u góry która to 305linja):

Cytat
Traceback (most recent call last):
File "C:\skrypt.py", line 897, in <module>
phpbb2_posts()
File "C:\skrypt.py", line 305, in phpbb2_posts
number = int(poster_ip[start:end])
ValueError: invalid literal for int() with base 10: ''

Z uwagi że nigdy nie zagłebiałem się w ten język może mi ktoś powiedzieć co tutaj jest nie tak ?
sztosz
Google wie więcej winksmiley.jpg Ale

int(x) w pythonie zmienia "x" (string najcześciej) na liczbę typu integer.

number = int(poster_ip[start:end])

"number" to jest nazwa zmiennej która ma przechowywać to czym jest "poster_ip[start:end]" ale w postaci liczby nie zaś np. stringu (ciągu znaków).

Error zaś mówi że "poster_ip[start:end]" nie składa się ze znaków z zakresu 0-9 więc nie może zmienić tego na liczbę.

poster_ip = mybb_posts[row][10].rstrip("'").lstrip("'") #ipaddress

zmienna poster_ip to chyba jakaś wartość wyciągnięta z bazy danych na 99% sądząc po komentarzu jest adres IP

rstrip i lstrip to są funkcje które usuwają dany znak z prawej i lewej strony danego ciągu znaków, w tym wypadku apostrof "'"

Moim zdaniem problem leży w tym że są jakieś śmieci w zmiennej poster_ip zamiast tego co powinno być, ale nie powiem Ci nic więcej, bo kod nie ma wcięć które powinien mieć jeśli to Python a mi się nie chce domyślać co gdzie i jak winksmiley.jpg
Grzesiek1
Jak by ktos był ciekaw co było źle to chodziło o cudzysłowia w pliku na którym operowała ta funkcja, poprawiłem i działa.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.