In: Computer Science
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.
--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]
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: