Python Forum
How to compressing and decompressing a list to a .csv file? Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to compressing and decompressing a list to a .csv file? Python
#1
Hello I was wondering if anyone can help me with this task, I've made an attempt but unsure how to complete, How would i go about compressing and decompressing a list to a .csv file? , see task below, Thanks

Develop a program that builds upon the technique from Task 2 to compress a text file with several sentences, including punctuation. The program should be able to compress a file into a list of words and list of positions to recreate the original file. It should also be able to take a compressed file and recreate the full text, including punctuation and capitalisation, of the original file.

Analyse the requirements for this system and design, develop, test and evaluate a program to compress a text file and reproduce the original text from a compressed file. You will need to create a text file with more than one sentence to test your system.


Code Below:

import csv 

alist = []
blist = []
clist = []
print("Welcome To This Program!")
word= input("enter sentence: ")
alist=word.split(" ")
for i in alist:
    if i not in blist:
        blist.append(i)
    clist.append(blist.index(i))
print(blist)
print("\n Positions: ")
print(clist)

fname = input("Please Enter File Name and Extension: ")
file=open(fname, 'w')
file.write(','.join(blist)+'\n'+','.join(str(i) for i in clist))
file.close
If anyone can help please let me know, Thanks.
Reply
#2
It looks like you have the decompression fine. The file writes correctly when I run it, although note that you need parents after file.close to actually call the close method.

Since you are importing csv, you might as well use csv.writer to write the file. Then you would use csv.reader to read the file. You can read each row as a list. They will be lists of strings, so you will need to use int to convert the indexes back to integers to reconstruct the sentence. Check out the csv documentation (https://docs.python.org/3/library/csv.html) and see if you can get it to work with that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-29-2016, 03:17 PM)n4meless Wrote:
fname = input("Please Enter File Name and Extension: ")
file=open(fname, 'w')
file.write("things")
file.close

To prevent yourself from making mistakes like this (never closing the file you opened), you should use with blocks to handle opening files, as they close the file for you when the file goes out of scope.
fname = input("Please enter file name and extension: ")
with open(fname, "w") as fout:
   fout.write("things")
Reply
#4
I decided to redo my code and managed to come up with this.

Please bare in mind that you do have to run the program 3 times in order to complete. Thanks for the help.
print("1. Write file")
print("2. Compress file")
print("3. Decompress file")

pos = []
list_words = []

u_input = int(input("Please Enter Number: "))
while u_input not in [1, 2, 3]:
  u_input= int(input("Please Enter A Number between 1 and 3: "))

#Writing To File
if u_input == 1:
  user_text= input("Please enter the text you would like to write to file: ")
  user_fname = input("Please enter a filename: ")
  file=open(user_fname+'.txt', 'w')
  file.write(user_text)
  file.close()

#Compressing
elif u_input == 2:
  fname = input("Please enter the name of the file you wish to compress: ")+ ".txt"
  fname = open(fname, 'r')
  sentence = fname.read()
  print("\n")
  print(sentence)
  print("\n")
  my_list = sentence.split(" ")
  print(my_list)
  print("\n")
  for word in my_list:
    if word not in list_words:
      list_words.append(word)
    pos.append(list_words.index(word))
  print(pos)

  pos= str(pos)
  list_words = str(list_words)

  file=open("compressed_file_words.txt", "w")
  file.write(list_words)
  file.close()
  file=open("compressed_file_word_pos.txt", "w")
  file.write(pos)
  file.close()

#Decompressing
elif u_input == 3:
  reconstructed_text = ""

  file=open("compressed_file_words.txt", "r")
  list_words =file.read()
  file.close()
  
  list_words = list_words.replace('[','')
  list_words = list_words.replace(']','')
  list_words = list_words.replace("',","")
  list_words = list_words.replace("'","")
  list_words = list_words.split()
  
  file=open("compressed_file_word_pos.txt", "r")
  pos = file.read()
  file.close
  
  pos = pos.replace('[','')
  pos = pos.replace(']','')
  pos = pos.replace(',','')
  pos = pos.split()
  
  integers = [int(j) for j in pos if j.isdigit()]
  print(integers)

  for i in integers:
    reconstructed_text += list_words[i] + " "

  print(reconstructed_text)
  
   
  
Reply
#5
If you use csv, the reading and writing is much simpler:

import csv 
 
test = [[2, 3, 5, 7], [11, 13, 17, 19]]
output = open('forum.txt', 'w')
writer = csv.writer(output)
for row in test:
    writer.writerow(row)
output.close()

infile = open('forum.txt')
reader = csv.reader(infile)
for row in reader:
    nums = [int(x) for x in row]
    print(nums)
    print(sum(nums))
infile.close()
Also, you can put everything in the same loop, so you don't have to rerun the program each time:

user_input = ''
while user_input ne 'q':
    user_input = ('Please enter (w)rite, (c)ompress, (d)ecompress, or (q)uit: ').lower()
    if user_input == 'w':
        # write
    elif user_input == 'c':
        # compress
    elif user_input == 'd':
        # decompress
    elif user_input != 'q':
        print('Please enter w, c, d, or q.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020