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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:33:08+00:00 2026-05-12T20:33:08+00:00

Not really too sure how to word this question, therefore if you don’t particularly

  • 0

Not really too sure how to word this question, therefore if you don’t particularly understand it then I can try again.

I have a file called example.txt and I’d like to import this into my Python program. Here I will do some calculations with what it contains and other things that are irrelevant.

Instead of me importing this file, going through it line-by-line and extracting the information I want.. can Python do it instead? As in, if I structure the .txt correctly (whether it be key / value pairs seperated by an equals on each line), is there a current Python ‘way’ where it can handle it all and I work with that?

  • 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-12T20:33:08+00:00Added an answer on May 12, 2026 at 8:33 pm
    with open("example.txt") as f:
        for line in f:
            key, value = line.strip().split("=")
            do_something(key,value)
    

    looks like a starting point if I understand you correctly. You need Python 2.6 or 3.x for this.

    Another place to look is the csv module that can parse comma-separated value files – and you can tell it to use = as a separator instead. This will abstract away some of the “manual work” in that previous example – but it seems your example doesn’t especially need that kind of abstraction.

    Another idea:

    with open("example.txt") as f:
        d = dict([line.strip().split("=") for line in f])
    

    Now that’s concise and pythonic 🙂

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

Sidebar

Related Questions

I am not really too sure how to word this into google, so I
I'm not too familiar with Linux/Bash, so I can't really find the right terms
NOTE: I am not exactly sure how to title or tag this question, so
I'm not really too sure how to best approach the following in python... I
I hope is not a question too specific... But I will be really thankfull
I'm not really sure to understand completely the factory pattern. Let's say I have
Not really getting the point of the map function. Can anyone explain with examples
(Not really a programming question, sorry) I'm working on benchmarking various filesystems (most importantly:
Not really a programming question, but relevant to many programmers... Let's say I have
Not really a coding question more a little help with my idea of a

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.