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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:05:16+00:00 2026-06-15T19:05:16+00:00

I know how to do it if its just two columns, but what if

  • 0

I know how to do it if its just two columns, but what if the file is like:

01 asd 023 green
01 dff 343 blue
02 fdf 342 yellow
02 fff 232 brown
02 kjf 092 green
03 kja 878 blue

and say, I would like column 2 to be the key to my dictionary and column 4 to my content for that key? I was thinking, a way to go around this problem would be to totally delete the other useless columns so that only the two I need remain, then I can use a script which I also saw on this website to make the dictionary

Python – file to dictionary?

Of course, this is a way around the problem, any tip is greatly appreciated.

  • 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-06-15T19:05:18+00:00Added an answer on June 15, 2026 at 7:05 pm
    d = {}
    with open('data.txt') as f:
      for line in f:
        tok = line.split()
        d[tok[1]] = tok[3]
    print(d)
    

    This produces

    {'kja': 'blue', 'kjf': 'green', 'fdf': 'yellow', 'asd': 'green', 'fff': 'brown', 'dff': 'blue'}
    

    split() (without an argument) splits the lines into lists of strings. tok[1] and tok[3] then use list indexing to address the second and fourth values in those lists, assigning them to a dictionary‘s keys and values (d[key] = value).

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

Sidebar

Related Questions

I couldn't find anything on this, but probably its just because I don't know
I'm new to regex and just want to know if its possible to find
Basically, I just want to know if its possible to use Nhibernate to migrate
I dont' event know if it's possible but here is what I would like
This might be a bit difficult to explain but I have two columns in
I don't know what its called so I'll just try to explain. Lets say
I have a DataGrid that two of its columns are ComboBoxes (one contains few
I know it's just late in the day and my brain is just fried....
If it's an int, I know it will be faster, just cannot understand the
I've done this a half dozen times so I know it's possible. I just

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.