Question

I am trying to retrieve all data from an sqlite3 database, format it and save it to a file, read that file and try to sort the records alphabetically then save that to a new file but It doesn't save them to the new file properly and i end up with more than one copy of that record. Can somebody help?

What I am trying to do:

  1. Retrieve records from a database
  2. Format these records
  3. Save to a (unsorted) file
  4. Take out the records from the (unsorted) file
  5. Sort the records alphabetically
  6. Save the (sorted) records to a new file

Text to avoid code after list bug

c.execute("SELECT * FROM Student, Behaviour")
    data = c.fetchall()
    currentRecords = open('Current Records - Unsorted', 'w')
    l = []
for i in data: #for individual records in the whole database do:
    record = str(i)
    record = record.replace("u'","")
    record = record.replace("'", "")
    record = record.replace('"', '')
    record = record.replace("(","")
    record = record.replace(")", "")
    record = record.replace(","," -")
    currentRecords.write(record+"\r\n")
    currentRecords.write('------------------------------------------------------------------------------'+"\r\n")
currentRecords.close()
y = open('Current Records - Unsorted','r')
z = y.read() #opening the file containing the unsorted, formatted records to read
l.append(z)
y.close() 
#sort the array alphabetically
def sort(l):
less = []
equal = []
greater = []
if len(l) > 1:
    pivot = l[0]
    for x in l:
        if x < pivot:
        less.append(x)
    elif x == pivot:
        equal.append(x)
    elif x > pivot:
        greater.append(x)
    return sort(less) + equal + sort(greater)
else:
    if len(l) == 1 or len(l) == 0:
        return l                        

 sortedCurrentRecords = sort(l)
 sortedCurrentRecordsFile = open('Current Records', 'w')
 for individualRecords in sortedCurrentRecords:
 sortedCurrentRecordsFile.write(individualRecords)
 sortedCurrentRecordsFile.close()   
 sortedCurrentRecordsFile1 = 'Current Records'
 subprocess.call(['open','-a','TextEdit', sortedCurrentRecordsFile1])

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top