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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:38:26+00:00 2026-06-01T19:38:26+00:00

I have a file which has about 25000 lines, and it’s a s19 format

  • 0

I have a file which has about 25000 lines, and it’s a s19 format file.

each line is like: S214 780010 00802000000010000000000A508CC78C 7A

There are no spaces in the actual file, the first part 780010 is the address of this line, and I want it to be a dict’s key value, and I want the data part 00802000000010000000000A508CC78C be the value of this key. I wrote my code like this:

def __init__(self,filename):
    infile = file(filename,'r')
    self.all_lines = infile.readlines()
    self.dict_by_address = {}

    for i in range(0, self.get_line_number()):
        self.dict_by_address[self.get_address_of_line(i)] = self.get_data_of_line(i)

    infile.close()

get_address_of_line() and get_data_of_line() are all simply string slicing functions. get_line_number() iterates over self.all_lines and returns an int

problem is, the init process takes me over 1 min, is the way I construct the dict wrong or python just need so long to do this?

And by the way, I’m new to python:) maybe the code looks more C/C++ like, any advice of how to program like python is 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-01T19:38:27+00:00Added an answer on June 1, 2026 at 7:38 pm

    This code should be tremendously faster than what you have now. EDIT: As @sth pointed out, this doesn’t work because there are no spaces in the actual file. I’ll add a corrected version at the end.

    def __init__(self,filename):
        self.dict_by_address = {}
    
        with open(filename, 'r') as infile:
            for line in infile:
                _, key, value, _ = line.split()
                self.dict_by_address[key] = value
    

    Some comments:

    • Best practice in Python is to use a with statement, unless you are using an old Python that doesn’t have it.

    • Best practice is to use open() rather than file(); I don’t think Python 3.x even has file().

    • You can use the open file object as an iterator, and when you iterate it you get one line from the input. This is better than calling the .readlines() method, which slurps all the data into a list; then you use the data one time and delete the list. Since the input file is large, that means you are probably causing swapping to virtual memory, which is always slow. This version avoids building and deleting the giant list.

    • Then, having created a giant list of input lines, you use range() to make a big list of integers. Again it wastes time and memory to build a list, use it once, then delete the list. You can avoid this overhead by using xrange() but even better is just to build the dictionary as you go, as part of the same loop that is reading lines from the file.

    • It might be better to use your special slicing functions to pull out the “address” and “data” fields, but if the input is regular (always follows the pattern of your example) you can just do what I showed here. line.split() splits the line on white space, giving a list of four strings. Then we unpack it into four variables using “destructuring assignment”. Since we only want to save two of the values, I used the variable name _ (a single underscore) for the other two. That’s not really a language feature, but it is an idiom in the Python community: when you have data you don’t care about you can assign it to _. This line will raise an exception if there are ever any number of values other than 4, so if it is possible to have blank lines or comment lines or whatever, you should add checks and handle the error (at least wrap that line in a try:/except).

    EDIT: corrected version:

    def __init__(self,filename):
        self.dict_by_address = {}
    
        with open(filename, 'r') as infile:
            for line in infile:
                key = extract_address(line) 
                value = extract_data(line)
                self.dict_by_address[key] = value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .txt file which has about 500k entries, each separated by new
We have a .aspx file which has about 400 lines of javascript code. Is
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have an XML file which has many section like the one below: <Operations>
I have file about 4MB (which i called as big one)...this file has about
I have a javascript source code file which is about 32kb, and has javascript
I have a text file which has first line as below: j0W82LBrSdUbw Basically it
I have an XML file that is approximately 12mb which has about 16000 product's.
everyone, I am dealing with a log file which has about 5 million lines,
I have a log file which has a format of this kind: DATE-TIME ###

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.