Question

In: Computer Science

Python 3 A simple way to encrypt a file is to change all characters following a...

Python 3

A simple way to encrypt a file is to change all characters following a certain encoding rule. In this question, you need to move all letters to next letter. e.g. 'a'->'b', 'b'->'c', ..., 'z'->'a', 'A'->'B', 'B'->'C', ..., 'Z'->'A'. For all digits, you need to also move them to the next number. e.g. '0'->'1', '1'->'2', ..., '9'->'0'. All the other symbols should not be changed.

  1. Write a function encrypt with the following requirements:
  • the function takes a string argument, which is a file name.
  • read the csv file.
  • replace all characters uisng the rule above.
  • write the content to a new file named "encrypted.csv".
  1. Call the function with the file name "business-price-indexes-june-2020-quarter-csv-corrected.csv"

--2020-10-16 19:32:31-- https://www.stats.govt.nz/assets/Uploads/Business-price-indexes/Business-price-indexes-June-2020-quarter/Download-data/business-price-indexes-june-2020-quarter-csv-corrected.csv Resolving www.stats.govt.nz (www.stats.govt.nz)... 45.60.11.104 Connecting to www.stats.govt.nz (www.stats.govt.nz)|45.60.11.104|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 11924606 (11M) [text/csv] Saving to: ‘business-price-indexes-june-2020-quarter-csv-corrected.csv’ business-price-inde 100%[===================>] 11.37M 4.56MB/s in 2.5s 2020-10-16 19:32:34 (4.56 MB/s) - ‘business-price-indexes-june-2020-quarter-csv-corrected.csv’ saved [11924606/11924606]

Solutions

Expert Solution

I have added the comments at every line for your understaning and also attaching the screenshots below for clarification.

for test sample i have used input.csv file as input and output_1.csv file as output you can change those names as per your requirement.


#importing the writer and reader libraries for csv file in python
from csv import writer
from csv import reader
# importing the module called csv which will be used  
import csv 

#for manpulating the alphabets during encryption we used this lib
from string import ascii_letters


#Encryption method as per your requirement
def Encrypt(Input_csv_filename):
  # we will open the input_file in read mode and our required output_file in write mode
  with open(Input_csv_filename, 'r') as reading_obj, \
          open('output_1.csv', 'w', newline='') as writing_obj:
      # we are creating a csv.reader_1 object from the given input file object which will be used to read
      csv_reader_1 = reader(reading_obj)
      # we are creating a csv.writer_2 object from the desired output file object which will be used to write
      csv_writer_2 = writer(writing_obj)
      # we are reading each row of the input of the given csv file as list
      for row in csv_reader_1:
        # we are parsing each column of a row 
        for col in row: 
          #two temporary strings to store 
          str_temp = col
          str_temp_2=''
          #replacing logic comes here as per your requirement
          for c in str_temp:
            if c == 'z':
              str_temp_2 += 'a'
            elif c == 'Z':
              str_temp_2 += 'A'
            elif ((ord(c) == 57)):
              str_temp_2 += '0'
            elif ((ord(c) >= 48) and (ord(c) < 57)):
              str_temp_2 += chr((ord(c)+1))
            elif c in ascii_letters:
              str_temp_2=str_temp_2+ascii_letters[(ascii_letters.index(c)+1)%len(ascii_letters)]
            else:
              str_temp_2+=c
          col=str_temp_2
          
      #priniting in the console output just for your understanding
        print(col)
        csv_writer_2.writerow([col])
    
Encrypt("input.csv")

output: (in console)

input file :

output file:


Related Solutions

Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
How do I write a program in MIPS that will change all the characters in a...
How do I write a program in MIPS that will change all the characters in a string into lowercase. For instance: string: " CANBERRA AUSTRALIA" Output: "canberra australia"
2. What characters do all plants and green algae share? 3. What characters do all plants...
2. What characters do all plants and green algae share? 3. What characters do all plants share that green algae do not share? 4. What is meant by alternation of generations? 5. What are the terrestrial adaptations that plants have evolved and which groups share those characters? 6. Compare and contrast phloem and xylem. 7. Compare and contrast the life cycles of the four groups of plants. The sporophyte generation compared to the gametophyte generation. Which is dominant in each...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the code below in the new file (you may omit the comments, I included them for explanation #Python Code Begin x = int(input("Enter a number: ")) y = int(input("Enter another number: ")) print ("Values before", "x:", x, "y:", y) #add code to swap variables here #you may not use Python libraries or built in swap functions #you must use only the operators you have learned...
In Python, accept shoter names and pad them with X’s to fill out the 3 characters...
In Python, accept shoter names and pad them with X’s to fill out the 3 characters needed. So for example >>> print(user_name("Bo", "Li")) BoXLiX >>> )
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line...
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line using nested loops. Here is what you need to know: 1- Copy this and don't change the inputs in the provided code: # Get the size and drawing character from the user size = input('Please enter the size: ') # Validate the input, exit if bad if size.isdigit(): size = int(size) else: print("Exiting, you didn't enter a number:", size) exit(1) # Input the drawing...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Change the Homework.java file in the ways indicated in the TODOs such that it prints all...
Change the Homework.java file in the ways indicated in the TODOs such that it prints all true values. The way I've given you the code it prints false for everything. You're going to use &, |, >> and << to change numbers. Submit: 1. Your completed Homework.java ---- public class Homework {    /**    * TODO: change bitmask1 and bitmask2 so that this prints true twice.    */    public static void usingAND() {        System.out.println("Testing & ...");...
Explain how and why the following characters in “King Arthur” change in the course of this...
Explain how and why the following characters in “King Arthur” change in the course of this myth: (a) Arthur; (b) Lancelot; (c) Guinevere; (d) Gawain; (e) Mordred. Do you see yourself in any of these characters? How so?
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT