Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7441157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:57:59+00:00 2026-05-29T10:57:59+00:00

I am still learning Python and as a little Project I wrote a script

  • 0

I am still learning Python and as a little Project I wrote a script that would take the values I have in a text file and insert them into a sqlite3 database. But some of the names have weird letter (I guess you would call them non-ASCII), and generate an error when they come up. Here is my little script (and please tell me if there is anyway it could be more Pythonic):
import sqlite3

f = open('complete', 'r')
fList = f.readlines()
conn = sqlite3.connect('tpb')
cur = conn.cursor()

for i in fList:
    exploaded = i.split('|')
    eList = (
        (exploaded[1], exploaded[5])
    )
    cur.execute('INSERT INTO magnets VALUES(?, ?)', eList)
    conn.commit()
cur.close()

And it generates this error:

Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\sortinghat.py", line 13, in <module>
    cur.execute('INSERT INTO magnets VALUES(?, ?)', eList)
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a te
xt_factory that can interpret 8-bit bytestrings (like text_factory = str). It is
highly recommended that you instead just switch your application to Unicode str
ings.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:57:59+00:00Added an answer on May 29, 2026 at 10:57 am

    To get the file contents into unicode you need to decode from whichever encoding it is in.
    It looks like you’re on Windows so a good bet is cp1252.
    If you got the file from somewhere else all bets are off.

    Once you have the encoding sorted, an easy way to decode is to use the codecs module, e.g.:

    import codecs
    # ...
    with codecs.open('complete', encoding='cp1252') as fin: # or utf-8 or whatever
      for line in fin:
        to_insert = (line.split('|')[1], line.split('|')[5])
        cur.execute('INSERT INTO magnets VALUES (?,?)', to_insert)
        conn.commit()
    # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still new to python and django, though learning ;-) I have a view that
im currently learning python (in the very begining), so I still have some doubts
I'm still learning python. I just wrote this method to determine if a player
I'm still learning python and I have a doubt: In python 2.6.x I usually
Still learning django and python... I have a form with many fields. I want
I'm still learning Grails and seem to have hit a stumbling block. Here are
I'm still learning Python, and I'd love to know a way to make the
I'm still learning python and after playing around with pygame I noticed I'm re-importing
I have started learning Python by writing a small application using Python 3.1 and
I have been learning Python by following some pygame tutorials . Therein I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.