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
#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


Messages In This Thread
RE: How to compressing and decompressing a list to a .csv file? Python - by ichabod801 - Dec-30-2016, 02:37 PM

Forum Jump:

User Panel Messages

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