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

  • SEARCH
  • Home
  • 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 5945339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:40:03+00:00 2026-05-22T16:40:03+00:00

I’m doing a task for uni, where we take a text file with a

  • 0

I’m doing a task for uni, where we take a text file with a list of student IDs and their favourite movies, games, tv shows, et cetera, and populating a MySQL database with it. The text files are formatted like so:

1   Fight Club
1   Pootie Tang
3   The Lord Of The Rings Trilogy
3   Ocean's Eleven
3   The Italian Job
4   Apocalypse Now
4   Black Hawk Down
4   Full Metal Jacket
4   Platoon
4   Star Wars Series
4   Stargate
4   The Crow

and the database table (popularity) is this:

studentnumber (int)
category      (varchar)
value         (varchar)

My code for doing all this is as follows:

import MySQLdb

def open_connection():
    '''Returns the database object'''
    connection = MySQLdb.connect(host='localhost',
                                 user='root',
                                 passwd='inb104',
                                 db='inb104')
    return connection


def process_file(category, cursor):
    '''opens the relavent file, then for each entry, runs a query to insert it
    into the database'''
    words = open(category + '.txt', 'rU')
    entries_list = []

    for line in words:
        split_line = line.split()
        number = str(split_line[0])
        value = str(split_line[1])
        entries_list.append((number, category, value))

    cursor.executemany('''INSERT INTO popularity
                          VALUES (%s, %s, $s)''', entries_list)


def data_entry(categories):
    '''
    Adds data from each category in a list of categories into a MySQL
    database.  A text file exists for each category in the list.

    categories is a list of category strings.
    '''
    connection = open_connection()
    cursor = connection.cursor()

    #process the file
    for item in categories:
        process_file(item, cursor)

if __name__ == '__main__':
data_entry(['movies', 'sports', 'actors', 'tv', 'games', \
            'activities', 'musicians', 'books'])

The problem I’m having is that no matter what I do, I keep getting the following error:

Traceback (most recent call last):
  File "\\vmware-host\Shared Folders\Uni\INB104\portfolio2\data_entry\data_entry_q.py", line 96, in <module>
    'activities', 'musicians', 'books'])
  File "\\vmware-host\Shared Folders\Uni\INB104\portfolio2\data_entry\data_entry_q.py", line 78, in data_entry
    process_file(item, cursor)
  File "\\vmware-host\Shared Folders\Uni\INB104\portfolio2\data_entry\data_entry_q.py", line 63, in process_file
    VALUES (%s, %s, $s)''', entries_list)
  File "C:\Python25\Lib\site-packages\MySQLdb\cursors.py", line 212, in executemany
    self.errorhandler(self, TypeError, msg)
  File "C:\Python25\Lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting

I have no idea how to fix this (even after extensive googling), and I can’t change from MySQLdb because it’s a uni task (personally, i’d be using sqlalchemy). If anyone can help it’s be greatly appreciated!

Thanks,
Tom

  • 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-22T16:40:04+00:00Added an answer on May 22, 2026 at 4:40 pm

    Its must be the typo, remove the $ and replace it with %

    Error code:

    cursor.executemany('''INSERT INTO popularity
          VALUES (%s, %s, $s)''', entries_list)
    

    Corrected code:

    cursor.executemany('''INSERT INTO popularity
          VALUES (%s, %s, %s)''', entries_list)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.