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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:42:03+00:00 2026-05-28T04:42:03+00:00

I’m currently working on a script which extracts data from two sources where one

  • 0

I’m currently working on a script which extracts data from two sources where one of those is Norwegian post codes. Norwegian post codes are made up of four digits and some begins with a zero.

Here is the code:

#This section loads data on Norwegian post codes and places into a dictionary where postcode is key
f = open("postoversikt.txt", "r");
f1 = open("PCODES_USER_TRIM.txt","r") #load the file with all the users. 
fo = open("pcodes_out","w")
place = {}
times = {}
for line in f:
    words = line.rsplit("\t");
    place[str(words[0])] = words[1]; #Reverse these to change the key and value - Default key: postcode value: place

number = 0;
number_unique = 0;
number_alike = 0;

for line in f1:
    number = number + 1;
    words1 = line.rsplit(";");
    if not words1[1] in times:
        number_unique = number_unique + 1;
        times[words1[1]] = 1;
    else: 
        number_alike = number_alike + 1;
        times[words1[1]] = times[words1[1]] + 1;

for key, value in times.items():
     print key+";"+value+";"+words[key];
     fo.write(key+";"+value+";"+words[key]+"\n");


print "Totalt antall objekter behandlet er: "+ str(number);
print "Hvorav antall unike var: "+ str(number_unique);
print "Antall like nummer ble funnet: " + str(number_alike);

Some lines from PCODES_USER_TRIM:

75621;4517;45 - 65
35214;7650;25 - 45
55624;9015;25 - 45
09523;5306;45 - 65
09051;2742;25 - 45
88941;1661;18 - 25

Some lines from postoversikt.txt:

0001    OSLO    0301    OSLO    P
0010    OSLO    0301    OSLO    B
0015    OSLO    0301    OSLO    K
0016    OSLO    0301    OSLO    K
0017    OSLO    0301    OSLO    K
0018    OSLO    0301    OSLO    G
0021    OSLO    0301    OSLO    K
0022    OSLO    0301    OSLO    K

One of the problems that occur is that the postcodes that begins with a zero is striped of the initial zero. My guess is that this is due to an internal conversion to an int (I’m just a beginner in Python, so please forgive if my problems are a bit mundane). I would like these to be in the standard format of four numbers xxxx. My second problems which I guess follows from my first is that I want to add the name of the post code to the final print out. This doesn’t work as I can’t use the key to refer to the place in words.

I used to convert the object I print to Strings using the str() method, but I refrained from doing so in the current version as I want to handle the problem by its root.

Could someone please help me with my little problem? How could I use rsplit to put Strings into the words dictionary without converting it to integers?

  • 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-28T04:42:03+00:00Added an answer on May 28, 2026 at 4:42 am

    Python is "strongly typed" and does not automatically coerce key types, or any types for that matter:

    >>> d = {'01234':'value'}
    >>> print d.items()
    [('01234', 'value')]
    

    I don’t see anything in your code that does conversion to int, but I’m pretty sure this is not the code you are using because it contains at least one syntax error:

     fo.write("key+";"+value+";"+words[key]\n")
    

    Please paste the actual code you are using.

    Additionally, give us a few lines from the input documents and their formats, so we don’t have to guess.

    EDIT:

    This code will do what you want. Again, there’s no sign of leading zeros being lost…

    places = {}
    for line in f:
        post, place, _rest = line.split('\t',2)
        places[post] = place
    f.close()
    
    times = {}
    for line in f1:
        _id, post, _rest = line.split(';',2)
        times[post] = times.get(post, 0) + 1
    f1.close()
    
    for k,v in times.iteritems():
        fo.write("%s;%s;%s\n" % (k,v,places[k]))
    fo.close()
    
    number = sum(times.itervalues())
    number_unique = len(times)
    number_alike = number - number_unique
    
    print number, number_unique, number_alike
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.