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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:53:20+00:00 2026-06-17T16:53:20+00:00

I am working on a fairly basic encoder/decoder where you can input your own

  • 0

I am working on a fairly basic encoder/decoder where you can input your own text file (as a string) and your own encoder (also as a string: it must be a text file).

Here is my decoder function:

def cDecode(file_name, encoder='standard_encoder.txt', save_new=True): # does not decode multi-lines correctly -- everything goes on a single line. See next comment
    '''Decodes <'file_name'> with the reverse method of <'encoder'>.'''
    if type(file_name) != str or type(encoder) != str: raise TypeError("<'file_name'> and <'encoder'> must be of type <'str'>.")
    if type(save_new) != bool: raise TypeError("<'save_new'> must be of type <'bool'>.")
    if file_name[-4:] != '.txt': file_name += '.txt'
    if encoder[-4:] != '.txt': encoder += '.txt'
    decoder_set = {}
    try:
        with open(encoder, 'r') as encoding_file:
            for line in encoding_file:
                line_parts = line.split(': ')
                my_key, my_value = line_parts[1], line_parts[0]

I think the error is in here:
I have to remove the ‘\n’ because every character (in the decoding file) is on a new line, like such: ‘A: Ð’.

                if '\n' in my_key:
                        loc = my_key.find('\n') # this may be the cause of the single-line of the decoding.
                        my_key = my_key[:loc] + my_key[loc + 1:]
                decoder_set[my_key] = my_value
        encoding_file.close()
    except IOError:
        encoder = 'standard_encoder.txt'
        with open(encoder, 'r') as encoding_file:
            for line in encoding_file:
                line_parts = line.split(': ')
                my_key, my_value = line_parts[1], line_parts[0]
                # every key has a new line character automatically because it's on a different line
                if '\n' in my_key:
                        loc = my_key.find('\n')
                        my_key = my_key[:loc] + my_key[loc + 1:]
                decoder_set[my_key] = my_value
        encoding_file.close()
    decodingKeys = decoder_set.keys()

Here is the rest of the function:

    if save_new:
        try:
            decoded_file_name = file_name[:-12] + '_decoded' + file_name[-4:]
            encoded_file = open(decoded_file_name, 'a+')
            with open(file_name, 'r') as my_file:
                for line in my_file:
                    de_line = ''
                    for char in line:
                        if char in decodingKeys: de_char = decoder_set[char]
                        else: de_char = char
                        de_line += de_char
                    encoded_file.write(de_line)
        except IOError:
            raise NameError(file_name + ' was not found. Decoding process terminated.')
    else:
        try:
            import os
            encoded_file = file_name[:-12] + '_decoded' + file_name[-4:]
            with open(file_name, 'r+') as my_file:
                for line in my_file:
                    de_line = ''
                    for char in line:
                        if char in decodingKeys: en_char = decoding_set[char]
                        else: de_char = char
                        de_line += de_char
                    encoded_file.write(de_line)
                os.remove(file_name)
                os.rename(encoded_file, file_name)
        except IOError:
            raise NameError(file_name + ' was not found. Decoding process terminated.')

Say I have a multi-line text-file:

This is a test.
As is this one.
Good bye!

When encoded and then decoded afterward, it shows up like this: This is a test.As is this one.Good bye!.

How can I fix this? I’m expecting it to show up like:

This is a test.
As is this one.
Good bye!

Thanks!

  • 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-17T16:53:21+00:00Added an answer on June 17, 2026 at 4:53 pm

    Add a '\n' while writing back the line to file:

    encoded_file.write(de_line+'\n')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a fairly basic screen layout for my first Android application
I have a menu working fairly well but can't figure out how to make
I've been working on a Javascript library, one with basic functionality. I'm fairly new
I am working a fairly basic web based KPI system and am struggling to
I am working on a countdown timer written in Javascript. Fairly basic really. Just
I've been working on sort of logging to text file using BufferedWriter and I
I'm trying to do something which seems fairly basic, but can't seem to get
I am working with a fairly old web app that uses MySQL as the
I'm working on a fairly large project for a trading company in Philadelphia. The
I'm working on a fairly large web site built in PHP that will potentially

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.