मैं एक नई फ़ाइल के लिए एक सूची की एक सूची लिखने के लिए कोशिश कर रहा हूँ, लेकिन मैं इस त्रुटि हो रही है:लेखन त्रुटि: उम्मीद एक चरित्र बफर वस्तु
Traceback (most recent call last): File "", line 1, in dowork() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 11, in dowork WriteFile() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 71, in WriteFile f.write(thefile) TypeError: expected a character buffer object
मैं करने के लिए एक सूची की एक सूची कैसे लिख सकता हूँ एक पंक्ति?
यह मैं कैसे लिख रहा हूँ है:
def WriteFile():
global thefile
f = open("output"+'.csv','w')
f.seek(0)
f.write(thefile)
f.close()
और यहाँ अगर आप इसे जरूरत पूरा स्रोत है:
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)))
आपको वास्तव में ग्लोबल वैरिएबल को एक चर के साथ बदलना चाहिए जिसे आप प्रत्येक फ़ंक्शन में पास करते हैं, और जब आप उनके साथ काम करते हैं तो फ़ाइलों को बंद करें। और अब जब हम पूरा कोड देख सकते हैं, तो मैं दृढ़ता से सुझाव दूंगा कि आपके पास 'प्रोसेसलाइन' फ़ंक्शन है (आप केवल 'लाइन' और' कंपनी' लूप को उलट सकते हैं), और बस लाइन से लाइन फ़िल्टर करें, ताकि आप किसी सूची की आवश्यकता नहीं है, और इन्हें सूची में से चीजों को हटाने की आवश्यकता नहीं है, और इसी तरह। पूरी बात इस तरह से बहुत आसान होगी। – abarnert
संभव डुप्लिकेट [TypeError: अपेक्षित एक वर्ण बफर ऑब्जेक्ट - टेक्स्टफाइल में पूर्णांक को सहेजने का प्रयास करते समय] (http: // stackoverflow।कॉम/प्रश्न/9786 9 41/टाइपर-अपेक्षित-ए-कैरेक्टर-बफर-ऑब्जेक्ट-जबकि-कोशिश-टू-सेव-इंटीजर-टू -0) –