0
Please please help
I have code that indentifies words in the user's sentence and stores them in a list. I then created a list of positions for words in that list. Then saved these lists as a file. The second part must follow on and 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. Im not sure how to do the second part.
6 Respostas
0
It depends on how you stored the words into list.
for eg. list=['hii','hello','fine']
you want to store it into a file right? though i didn't get your query about compressing. If you want to compress in python then you have to use 'zlib' module of python.
Coming back to saving it into a text file:
import io
fp=open("file.txt","w")
for line in list:
fp.write(line+"\n")
It will write all the words in your list into the text file , each word on a new line , though u can modify the write() if u want save the words into sentences.
0
All that has been done i just need to follow on from that and do the compression which i dont understand
0
Okay so for compressing files or text there are many modules in python like zlib,gzip,etc...
I use gzip as it is much more compact and i use linux so ;)
import gzip
fopenfile = open('file.txt') #which earlier we created
fwritefile = gzip.open('file.txt.gz', 'wb')#.gz is the extension
fwritefile.writelines(fopenfile)
fwritefile.close()#always close what u open it is a good programming practice
fopenfile.close()
There is a another way using gzip but this is easy to understand.
0
I have already imported a system at the start of my program, can i still import gzip?
0
Yes you can import as many modules as you can , but in this program we are opening the same file two times. Once you close a file you cant open it in the same file. But you can use functions for opening the same file number of times in a same file.
So create two functions one for writing into a text file and other for compressing.
Otherwise write a different program for compression.
0
Thank you! We had to compress a text file with several sentences including punctuation. It should be able to compress a file into a list of words and list of positions to recreate the original file. And should be able to take a compresses file and recreate the full text of the original file. It just sounded confusing 😂