![]() |
Compression/Decompression Code not working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Compression/Decompression Code not working (/thread-3054.html) |
Compression/Decompression Code not working - Zanbezi - Apr-26-2017 My code is supposed compress a text file into numbers and be able to save it to a file.Its also meant to be able to decompress a file and save the results in a file. If someone could help me fix this without drastically changing the code I would be grateful. import re #imports library re import csv #imports csv library so I can write to documents def Menu(): while True: #menu is looped untill user gives valid data print ("Press 1 for Compression , Press 2 for Decompression, Press 3 to Quit")#options are displayed to user choice = input("Please choose an option: ") #user is asked to choose an option if choice == "1":#if user chooses one Compress funtion is run Compress() elif choice == "2":#if user chooses two Deompress funtion is run Decompress() elif choice == "3":#if user chooses three Compress program quits quit() else: print("Enter a valid choice")#if user enters an invalid choice they are promted to enter valid data def Sentence(filename): f = open(filename, "r")#open file user has chosen in read mode sentence = f.read() #the data from the file is saved as sentence sentenceList = re.split("(\W)", sentence)#conerts saved data from file into list if "\n" in sentenceList: #unformats the file to make the list cleaner sentenceList.remove("\n") return sentenceList#returns variable so can be used later def Words(sentence): UniqueWords = [] #empty list called Uniquewords defined for word in sentence: #loops through sentence one word at a time if word not in UniqueWords:#if a word is not in list UniqueWords.append(word)#adds the word to list return UniqueWords#returns variable so can be used later def Numbers(sentence, UniqueWords): #fucntion takes variables from other fucntions NumbersList = []#empty list called Nuberlist defined for word in sentence: #loops through sentence to find word position num = UniqueWords.index(word) NumbersList.append(num+1)#number added to count return NumbersList#returns variable so can be used later def Output(UniqueWords, NumbersList): valid = False while valid == False:#loop create filename = input("What is the name of the file the data will be saved to: ")#asks the user for the name of their file filename += ".txt"#adds .txt to end of file if filename[0] != ".": #checks that name doesnt begin with dot or is a blank valid = True else: print ("Your filename is invalid as it starts with a dot or is blank. Please try again.") if valid == True: if filename[0] == " ":#checks if name begins with space valid = False print("Your filename is invalid as it begins with a space.Please try again.") files=open(filename,"w")#open/creates the user has specified lists=csv.writer(files) lists.writerow(NumbersList)#utilises variables from previous code to write data to file lists.writerow(UniqueWords) files.close()#closes file return filename#returns file def Input(): valid = False while valid == False:#loop created filename = input("What is the name of the file you want to Compress/Decompress: ")#asks user for file to Decompress/Compress filename += ".txt"#adds txt to users selcted file if filename[0] != ".":#checks that name doesnt begin with dot or is a blank valid = True else: print ("Please retry as you filename is invalid due to their being a dot or blank.") if valid == True: if filename[0] == " ":#checks if filename begins with space valid = False print("Your filename begins with a Space please retry") if valid == True: try: f = open(filename, "r") f.close() except: print ("File is in wrong format or doesnt exits Please retry ") valid = False f.close()#closes file return filename #returns variable def write(inputfile): files=open(inputfile,"r")#opens file user has defined data=files.read()#opens file in read mode datalines=data.splitlines()#splits the file data into lines numbers=datalines[0].split(",") #splits into commas files.close()#closes file return numbers#returns variable so can be used later def GetData(inputname): f = open(inputname, "r")#opens file user has defined reader = csv.reader(f) nums = next(reader) words = next(reader) f.close()#closes file return nums, words#returns variable so can be used later def output(numbers, words):#this function changes the orders of numbers so that the final data displayed will be correct outputstring = "" numbers =reversed(numbers) for char in numbers: outputstring = words[int(char)-1] + outputstring outputstring = outputstring.replace('\"', "") outputstring = outputstring.replace('\'', "") return outputstring def Compress(): inputfile = Input() #takes variable from other funtion to be used sentence = Sentence(inputfile)#takes variable from other funtion to be used UniqueWords = Words(sentence)#takes variable from other funtion to be used NumbersList = Numbers(sentence, UniqueWords)#takes variable from other funtion to be used Filename = Output(UniqueWords, NumbersList)#takes variable from other function to be used print ("Done, the data", UniqueWords, "and", NumbersList, "saved to file", Filename)#displays data and confirms operation was sucessful def Decompress(): inputfile = Input() try: GetNumbers, GetWords = GetData(inputfile) MakeOutput = output(GetNumbers, GetWords)#takes variable from other function to be used Filename = Output(UniqueWords, NumbersList) print ("Done!, file", inputfile, "has data \"" + MakeOutput + "\"" "has been saved to",Filename)#displays data and confirms operation was sucessful except: print("Error. File could not be found or was invalid format.") Menu()#runs menu fucntion RE: Compression/Decompression Code not working - sparkz_alot - Apr-26-2017 You need to repost your code without any formatting. Try using " Ctrl + Shift + v" to paste between the code tags. What exactly is it not doing? A better description would be appreciated. RE: Compression/Decompression Code not working - Zanbezi - Apr-27-2017 import re #imports library re import csv #imports csv library so I can write to documents def Menu(): while True: #menu is looped untill user gives valid data print ("Press 1 for Compression , Press 2 for Decompression, Press 3 to Quit")#options are displayed to user choice = input("Please choose an option: ") #user is asked to choose an option if choice == "1":#if user chooses one Compress funtion is run Compress() elif choice == "2":#if user chooses two Deompress funtion is run Decompress() elif choice == "3":#if user chooses three Compress program quits quit() else: print("Enter a valid choice")#if user enters an invalid choice they are promted to enter valid data def Sentence(filename): f = open(filename, "r")#open file user has chosen in read mode sentence = f.read() #the data from the file is saved as sentence sentenceList = re.split("(\W)", sentence)#conerts saved data from file into list if "\n" in sentenceList: #unformats the file to make the list cleaner sentenceList.remove("\n") return sentenceList#returns variable so can be used later def Words(sentence): UniqueWords = [] #empty list called Uniquewords defined for word in sentence: #loops through sentence one word at a time if word not in UniqueWords:#if a word is not in list UniqueWords.append(word)#adds the word to list return UniqueWords#returns variable so can be used later def Numbers(sentence, UniqueWords): #fucntion takes variables from other fucntions NumbersList = []#empty list called Nuberlist defined for word in sentence: #loops through sentence to find word position num = UniqueWords.index(word) NumbersList.append(num+1)#number added to count return NumbersList#returns variable so can be used later def Output(UniqueWords, NumbersList): valid = False while valid == False:#loop create filename = input("What is the name of the file the data will be saved to: ")#asks the user for the name of their file filename += ".txt"#adds .txt to end of file if filename[0] != ".": #checks that name doesnt begin with dot or is a blank valid = True else: print ("Your filename is invalid as it starts with a dot or is blank. Please try again.") if valid == True: if filename[0] == " ":#checks if name begins with space valid = False print("Your filename is invalid as it begins with a space.Please try again.") files=open(filename,"w")#open/creates the user has specified lists=csv.writer(files) lists.writerow(NumbersList)#utilises variables from previous code to write data to file lists.writerow(UniqueWords) files.close()#closes file return filename#returns file def Input(): valid = False while valid == False:#loop created filename = input("What is the name of the file you want to Compress/Decompress: ")#asks user for file to Decompress/Compress filename += ".txt"#adds txt to users selcted file if filename[0] != ".":#checks that name doesnt begin with dot or is a blank valid = True else: print ("Please retry as you filename is invalid due to their being a dot or blank.") if valid == True: if filename[0] == " ":#checks if filename begins with space valid = False print("Your filename begins with a Space please retry") if valid == True: try: f = open(filename, "r") f.close() except: print ("File is in wrong format or doesnt exits Please retry ") valid = False f.close()#closes file return filename #returns variable def write(inputfile): files=open(inputfile,"r")#opens file user has defined data=files.read()#opens file in read mode datalines=data.splitlines()#splits the file data into lines numbers=datalines[0].split(",") #splits into commas files.close()#closes file return numbers#returns variable so can be used later def GetData(inputname): f = open(inputname, "r")#opens file user has defined reader = csv.reader(f) nums = next(reader) words = next(reader) f.close()#closes file return nums, words#returns variable so can be used later def output(numbers, words):#this function changes the orders of numbers so that the final data displayed will be correct outputstring = "" numbers =reversed(numbers) for char in numbers: outputstring = words[int(char)-1] + outputstring outputstring = outputstring.replace('\"', "") outputstring = outputstring.replace('\'', "") return outputstring def Compress(): inputfile = Input() #takes variable from other funtion to be used sentence = Sentence(inputfile)#takes variable from other funtion to be used UniqueWords = Words(sentence)#takes variable from other funtion to be used NumbersList = Numbers(sentence, UniqueWords)#takes variable from other funtion to be used Filename = Output(UniqueWords, NumbersList)#takes variable from other function to be used print ("Done, the data", UniqueWords, "and", NumbersList, "saved to file", Filename)#displays data and confirms operation was sucessful def Decompress(): inputfile = Input() try: GetNumbers, GetWords = GetData(inputfile) MakeOutput = output(GetNumbers, GetWords)#takes variable from other function to be used Filename = Output(UniqueWords, NumbersList) print ("Done!, file", inputfile, "has data \"" + MakeOutput + "\"" "has been saved to",Filename)#displays data and confirms operation was sucessful except: print("Error. File could not be found or was invalid format.") Menu()#runs menu fucntion RE: Compression/Decompression Code not working - nilamo - May-10-2017 There's no indentation. It doesn't work, because it's not valid syntax. |