0

Hi i am a newbie to python.i am creating a small program which parses the load log file of a particular website and stores the valid data in particular feild of a database.but some of the feild has weired character like '推广频道'.(non-ascii character ' xe6').

i also tried to apply the solution of this question. Inserting unicode into sqlite? but this codec is unable to decode and providing UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 84: chara cter maps to

def db_log_entry(filename):

conn = sqlite3.connect('log.db')
c= conn.cursor()

""" creating log table
Table structure:
    Name          Type
    id            integer
    identifier    text
    url           text
    user_ip       text
    datetime      text
    timezone_mod  text
    query_type    text
    status        text
    opt_id        text
    user_agent    text
    opt_ip        text
    opt_ip_port   text
    """


c.execute(''' CREATE TABLE log
                (id integer, identifier text, url text, user_ip text, datetime text, timezone_mod text, query_type text, query_url text, status text,
                opt_id text, user_agent text, opt_ip text, opt_ip_port text)''')

f=codecs.open(filename,encoding='cp1252') ###   opening file to read data
loop_count=0         # variable to keep record of successful loop iteration
id_count=0           # variable to be entered in id feild of log table




""" Reading each line of log file for performing iterative opertion on each line to parse valid data
 make a entry in database """



for log_line in f:
    loop_count= loop_count+1
    id_count= id_count+1
    list=[]
    db_list=[]


    (txt1,txt2)=log_line.split('HTTP/')        #split the log_line in two parts txt1,txt2 for comparison with reg1,reg2


    reg1= r'(\d{6}_\d{5})\s([\w.-]+)\s([\d.]+)\s-\s-\s\[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2})\s([+-]?\d+)\]\s"(\w+)\s([\w.\-\/]+)'
    reg2= r'[1.1"]?[1.0"]?\s(\d*)\s(\d*)([\s\"\-]*)([\w\.\%\/\s\;\(\:\+\)\?\S\"]+)"\s(\d{2,3}.\d{2,3}.\d{2,3}.\d{2,3})\:(\d+)'

    print 'starting loop ',loop_count
    match= re.search(reg1,txt1)
    if match:                                  # If regex match found between (reg1,txt1) than append the data in db_list
        db_list.append(id_count)
        db_list.append(match.group(1))
        print match.group(1)
        db_list.append(match.group(2))
        print match.group(2)
        db_list.append(match.group(3))
        print match.group(3)
        db_list.append(match.group(4))
        print match.group(4)
        db_list.append(match.group(5))
        print match.group(5)
        db_list.append(match.group(6))
        print match.group(6)
        db_list.append(match.group(7))
        print match.group(7)

        print 'yes 1'
    else:
        print 'match not found in reg1'
    match= re.search(reg2,txt2)                 # If regex match found between (reg2,txt2) than append the data in db_list
    if match:
        db_list.append(match.group(1))
        print match.group(1)
        db_list.append(match.group(2))
        print match.group(2)
        #print match.group(3)
        db_list.append(match.group(4))
        print match.group(4)
        db_list.append(match.group(5))
        print match.group(5)
        db_list.append(match.group(6))
        print match.group(6)
        print 'yes 2'
    else:
        print 'match not found in reg2'






    print 'inserting value of',loop_count
    c.execute('INSERT INTO log VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)',db_list)
    conn.commit()
print 'success...'
conn.commit()


def main():

db_log_entry('access_log.txt')



if __name__ == '__main__':
  main()
3
  • 1
    Please don't post all your code and expect us to read through it all. Single out the part that's giving you the problem, and show that as a concise example.
    – pcalcao
    Commented Apr 12, 2013 at 14:21
  • 1
    Please include the full traceback of the exception. I strongly suspect it's not SQLite that is the problem here, but another aspect of your code. Commented Apr 12, 2013 at 14:23
  • the idea behind posting my whole code is that it will be easier for u guys to know the error vey well and u will be able to debug it easier Commented Apr 14, 2013 at 12:01

1 Answer 1

0

That's because you're using the wrong character encoding to open the file.

You should be opening it with UTF-8, like this:

f=codecs.open(filename,encoding='utf-8')

This is because CP-1252 is an encoding for the Latin alphabet and as such doesn't understand Japanese characters.

As I'm not sure what the original encoding (or language) is, UTF-8 is also a safe bet as it supports all languages.

1
  • My apologies if that is not Japanese. I can't tell the difference between Japanese/Chinese/Korean. Commented Apr 12, 2013 at 14:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.