TypeError: oczekiwany obiekt bufora znaków
Próbuję napisać listę listy do nowego pliku, ale otrzymuję ten błąd:
Śledzenie (ostatnie ostatnie wywołanie): Plik "", wiersz 1, w dowork () Plik "C: Python27 praca księgowanie formatowanie plików podręczników sdf.py", wiersz 11, w dowork WriteFile () Plik "C : Pyton27 praca księgowanie formatowanie plików podręczników sdf.py ", linia 71, w WriteFile f.write (thefile) TypeError: oczekiwany obiekt bufora znaków
Jak napisać listę listy do pliku?
Tak piszę:
def WriteFile():
global thefile
f = open("output"+'.csv','w')
f.seek(0)
f.write(thefile)
f.close()
a oto kompletne źródło, jeśli tego potrzebujesz:
import csv
thefile = []
output = []
def dowork():
sourceFile='e.csv'
thefile=ReadFile(sourceFile)
CleanFile(sourceFile)
ProcessFile()
WriteFile()
def ReadFile(filename):
return list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]
def CleanFile(sourceFile):
global thefile
thefiletmp=[]
for i, line in enumerate(thefile):
if line[2]=='':
del thefile[i]
else:
thefiletmp.append(line[4:])
thefile=thefiletmp
def ProcessFile():
global thefile
iCompany=1
iNum=0
iDate=2
iAging=3
iBalance=4
COMPANIES=GetDistinctValues(1)
mytemparray=[]
mytempfile=[]
for company in COMPANIES:
for line in thefile:
if line[iCompany]==company:
mytemparray.append(line[iCompany])
mytemparray.append(line[iNum])
mytemparray.append(line[iDate])
if line[2] in range(0,31):
mytemparray.append(line[iBalance])
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append('0')
if line[2] in range(31,61):
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytemparray.append('0')
mytemparray.append('0')
if line[2] in range(61,91):
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytemparray.append('0')
if line[2] >90:
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append('0')
mytemparray.append(line[iBalance])
mytempfile.append(mytemparray)
mytemparray=[]
thefile=mytempfile
def WriteFile():
global thefile
f = open("output"+'.csv','w')
f.seek(0)
f.write(thefile)
f.close()
def GetDistinctValues(theColumn):
return sorted(list(set(line[theColumn] for line in thefile)))