import csv

class Reader(object):
    def __init__(self):
        self.names = []
        self.idigit = []
        self.fileReader_list=[]
        self.fileReader = csv.reader(open("survey_result.csv", "rb"))
        self.fileReader_list.extend(self.fileReader)
        for column in self.fileReader_list:
            self.names.append(column[1])
            self.idigit.append(column[2])
        del self.names[0]
        del self.idigit[0]

X = Reader()
for i, names in enumerate(X.names):
    print names, X.idigit[i]

Ch, Sbb 6748
Oudfd, Sufdfdfy 8473
Sdfdfu, Radfdf 5667
Adfn, Mudfmmdfd 4290
Wodfdf, Yeddfdfon 8066
Chdfdng, Widfdfn 3325
Badfd, Sadfdf 4742


I want to sort them out. I tried
k = names, X.idigit then print k, however, this turns everything into a list, which I do not want to do.
Is there any alternative suggestion to do it?

I do this because I need to use textwrap later to formulate these data into a wrap table

Edited 7 Years Ago by jwxie: n/a

Change the class little,and show some way to handle data.

import csv

class Reader(object):
    def __init__(self):
        self.names = []
        self.idigit = []
        self.fileReader_list = []       
        self.fileReader = csv.reader(open("survey_result.csv", "rb"))
        self.fileReader_list.extend(self.fileReader)       
        
    def my_sort(self):
        for column in self.fileReader_list:
            self.names.append(column[0])
            self.idigit.append(column[1])       

x = Reader()
x.my_sort()
print 'Name | Number'
print x.names
print x.idigit
print
print 'String'
#make string
s_names = ','.join(x.names)
print s_names
print
print 'Sorted names'
#Sort list
print sorted(x.names)
print
#Make dictionary
my_dict = dict(zip(x.names, x.idigit))
print 'Dictionary'
print my_dict
print
print 'Dictionary test'
#Test dictionary
name = raw_input('Your name to get value: ')
if name in my_dict:
    print 'The value is:%s' % (my_dict[name])

''' my output-->
Name | Number
['Ch', 'Oudfd', 'Sdfdfu', 'Adfn', 'Wodfdf', 'Chdfdng', 'Badfd']
[' Sbb 6748', ' Sufdfdfy 8473', ' Radfdf 5667', ' Mudfmmdfd 4290', ' Yeddfdfon 8066', ' Widfdfn 3325', ' Sadfdf 4742']

String
Ch,Oudfd,Sdfdfu,Adfn,Wodfdf,Chdfdng,Badfd

Sorted names
['Adfn', 'Badfd', 'Ch', 'Chdfdng', 'Oudfd', 'Sdfdfu', 'Wodfdf']

Dictionary
{'Ch': ' Sbb 6748', 'Oudfd': ' Sufdfdfy 8473', 'Chdfdng': ' Widfdfn 3325', 'Adfn': ' Mudfmmdfd 4290', 'Wodfdf': ' Yeddfdfon 8066', 'Sdfdfu': ' Radfdf 5667', 'Badfd': ' Sadfdf 4742'}

Dictionary test
Your name to get value: Ch
The value is: Sbb 6748
'''
This article has been dead for over six months. Start a new discussion instead.