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 7053323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:29:28+00:00 2026-05-28T03:29:28+00:00

I have a tab-delimited txt file with rows separated by tabs, and rows separated

  • 0

I have a tab-delimited txt file with rows separated by tabs, and rows separated by newlines. This is what it actually looks like:

476502291\t\tLF3139812164\t\tTitle 1\tKids & Family\nGRAV_2011\t\tThe Full Picture\tIndependent\n [...etc...]

Note that sometimes values are separated by two tabs instead of one.

I need to insert this into a mysql table, which should result in the following:

ID             title               genre
476502291      Title 1             Kids & Family
GRAV_2011      The Full Picture    Independent

How would I read a tab-separated txt file and run a for loop in order to insert values into a table named vendor using MySQLdb?

>>> import MySQLdb
>>> conn = MySQLdb.connect (host = "localhost",
                             user = "me",
                             passwd = "password",
                             db = "my-db")
>>> cursor = conn.cursor ()
>>> # for loop  # how to read from the txt file to insert it as required?
>>>     # cursor.execute (INSERT...)
>>> conn.commit()
>>> conn.close()
  • 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-28T03:29:29+00:00Added an answer on May 28, 2026 at 3:29 am

    Step 1. Read the csv module. http://docs.python.org/library/csv.html. This does what you want.

    with open('your_data_file.dat','r') as source:
        rdr= csv.reader( source, delimiter='\t', quotechar='')
        for row in rdr:
            # you have your columns with which to do your insert.
    conn.commit()
    

    Step 2. Read up on context managers, also.

    from contextlib import closing
    
    with open('your_data_file.dat','r') as source:
        rdr= csv.reader( source, delimiter='\t', quotechar='')
        with closing(conn.cursor()) as cursor:
            for row in rdr:
                # you have your columns with which to do your insert.
    conn.commit()
    

    This will assure that cursors and files are properly closed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this small xls file that's actually a tab-delimited txt file. In
The problem I have a tab delimited input file that looks like so: Variable
I have 600 tab-delimited .txt files that look like this: barcode gene.symbol value 1
Basically I have converted a tab delimited txt file into a list containing a
I have a .txt file with the following data: sampleF.txt --> (tab delimited) MSFT
I have a tab-delimited text file. I have split this into columns. Each of
I have a file that is tab delimited. I would like a powershell script
I have a tab-delimited list of pairs like this: apple yellow orange green apple
I have a tab delimited file of 4 columns and n number of rows.
I have got string containing windows path to file. It looks something like this:

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.