0

The code I have is this. I am trying to radix sort a list of numbers into a list of lists and then write that list of lists to a file, with each individual list on a different line. If I try to use f.write, it gives me a character buffer expected error.

from math import log

b = open("radix.in.txt").readlines()
a = [int(i) for i in b]

f = open("radix.out.txt", "w")
def getIndex(num, base, digit_num):
    return (num // base ** digit_num) % base  

def newLists(base):
    return [ [] for i in range(base) ]  

def order(q, base, digit_num):
    placehold = newLists(base)
    for num in a:
        placehold[getIndex(num, base, digit_num)].append(num)  
    return placehold

def radixSort(a, base):
    passes = 3
    new_list = a
    for digit_num in range(passes):
    new_list = order(new_list, base, digit_num)
    list_c = [str(i) for i in new_list]
    print list_c


radixSort(a, 10)

Putting f.write(list_c) in after the print list_c gives this traceback

Traceback (most recent call last):
  File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 29, in <module>
    radixSort(a, 10)
  File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 26, in radixSort
    f.write(list_c)
TypeError: expected a character buffer object
1
  • 1
    Can you post the full error? Commented Apr 16, 2013 at 20:13

4 Answers 4

3

list_c is a list-of-str. To convert that to a single str, use str.join():

f.write('\n'.join(list_c)+'\n')

Or, if you prefer, you can write each str individually:

for s in list_c:
  f.write(s+'\n')
Sign up to request clarification or add additional context in comments.

4 Comments

Both of these are just creating an empty text file.
@NolanCaldwell - With all due respect, I don't believe you. The first one can't produce an empty text file -- it will always print at least one blank line. Further, when I test these, they produce output files with 10 lines, the contents of which depend upon the input given. Perhaps the lines you add aren't being executed?
I totally believe that it should work. I am stumped as to why it doesn't. I added the code you supplied to the radixSort method before the print statement which would surely execute them, no? For both of your code samples, a text file named "radix.out.txt" is created but nothing(it could be an empty line) is in the file.
There are two possibilities: you write to the wrong file, foo.txt, thinking that you wrote to bar.txt (less likely), or your list is empty (most likely). That means list_c is empty, thus new_list is empty, which means order() returned an empty list. Investigate your code from there.
0

convert it to a string and use literal_eval to get it as an array while reading

f=open('file.txt','w')
a=[[1,2],[3,4],[4,5]]
b=str(a)
f.write(b)

and when you read,

import ast
f=open('file.txt','r')
a=f.read()
b=ast.literal_eval(a)

because the write() function can process only strings. Or convert the list of lists to a string and use write.

Comments

0

Sounds like it is a great chance to use csv module.

import csv
with open('numbers.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    for lst in list_of_lists:
        writer.writerow(lst)

Comments

0

As others have pointed out, you need to serialize the data in some way before you can write it to a file. You can pickle, json encode, xml encode, stick it in a database like sqlite, write it line by line, put it in a CSV file, and etc... The "best" method (there is no best) depends on what you plan to do with that file later. Do people need to read it? Do non-python programs need to read it? Is it large enough to be concerned about the amount of space it takes?

If you just want to save a list of integers and want it to be readable in many programming languages, I like json

import json
(... your other stuff here...)
new_list = order(new_list, base, digit_num)
json.dump(new_list, open('myfile.json', 'w'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.