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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:03:42+00:00 2026-06-01T02:03:42+00:00

I have a css file of this type col1 col2 AAA a 1 a1

  • 0

I have a css file of this type

col1    col2
AAA       
 a        1
  a1      1
  a2      1
 b        1
  b1      1
  b2      1

i am reading first col based on indentation,”AAA” has 0 no of spaces,”a” “b” has 1 space and “a1″,”a2” “b1” “b2” has 2 space, now i am printing dict as

d={'a':['a1','a2'],'b':['b1','b2']}

But what i want is

d={'AAA':['a','b'],'a':['a1','a2'],'b':['b1','b2']}

i am using code like this

reader=csv.DictReader(open("c:/Users/Darshan/Desktop/sss.csv"),dialect="excel")
for row in reader:
    a.append(row['col1'])
    for i in range(len(a)):
        if a[i].count(' ')==1:
            d[a[i]]=[]
            k=a[i]

        else a[i].count(' ')==2:
            d[k].append(a[i])

this print this output

 d={'a':['a1','a2'],'b':['b1','b2']}

so can anyone help me,thanks in advance

  • 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-01T02:03:43+00:00Added an answer on June 1, 2026 at 2:03 am

    What if you just change your for loop to this:

    # A variable to keep track of the least-nested level of your hierarchy
    top_lvl = ''
    k = ''
    for i in range(len(a)):
        # Pre-compute this value so you don't have to do it twice or more
        c = a[i].count(' ')
        # This case is the topmost level
        if c == 0:
            top_lvl = a[i]
            d[top_lvl] = []
        # This case is the middle level
        elif c == 1:
            d[a[i]]=[]
            k=a[i]
            d[top_lvl].append(k)
        # This case is the most deeply nested level
        else: # c==2
            d[k].append(a[i])
    

    In fact now that I’m making everything all sweet, you can probably just iterate through the values in a directly, without referring to its values by index. Like so:

    # A variable to keep track of the least-nested level of your hierarchy
    top_lvl = ''
    # More descriptive variable names can make everything easier to read/understand
    mid_lvl = ''
    for val in a:
        # Pre-compute this value so you don't have to do it twice or more
        c = val.count(' ')
        # This case is the topmost level
        if c == 0:
            top_lvl = val
            d[val] = []
        # This case is the middle level
        elif c == 1:
            d[val]=[]
            mid_lvl =val
            d[top_lvl].append(mid_lvl)
        # This case is the most deeply nested level
        else: # c==2
            d[mid_lvl].append(val)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a css rule like this in my css file: .Island_VerticalMenu:first-child { width:
Imagine I have this: <div id=wrapper> </div> In my CSS file, when I type
I have this in my CSS file: body {style.css (line 3) background:#CDF8FF url(images/final_bg.jpg) no-repeat
I have a id based CSS file where I noticed I'm mostly repeating myself.
I have a dynamically generated CSS file. It's fairly large, and has different content
I have a templating css file type that allows comments to start with '##'
I have a CSS file, which contain the style information of my web. Some
I have a css file that I only want to be visible to Firefox
I have a HTML/CSS File laid out with a bunch of areas that need
If I have two css files: File 1: .colorme { background-color:Red; } File 2:

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.