<open file 'amount2.csv', mode 'r' at 0x1004656f0>
आप देख रहे हैं एक त्रुटि है, लेकिन अपने 'प्रिंट च' का परिणाम नहीं है।
मैं: बजाय अपने फ़ाइल की सामग्री को देखने के लिए, आप
with open('test.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
# row is a list of strings
# use string.join to put them together
print ', '.join(row)
आपकी फ़ाइल में पंक्तियों को संलग्न करने के लिए, बजाय पर Python CSV Docs
संपादित
changes = [
['1 dozen','12'],
['1 banana','13'],
['1 dollar','elephant','heffalump'],
]
with open('test.csv', 'ab') as f:
writer = csv.writer(f)
writer.writerows(changes)
अधिक जानकारी कर करना होगा पहली बार गलत समझा, आप अपनी सीएसवी फ़ाइल में '1 दर्जन' से '12' की सभी प्रविष्टियों को बदलना चाहते हैं। मैं पहले कहूंगा, सीएसवी मॉड्यूल का उपयोग किए बिना करना आसान है, लेकिन यहां इसका उपयोग करने का एक समाधान है।
import csv
new_rows = [] # a holder for our modified rows when we make them
changes = { # a dictionary of changes to make, find 'key' substitue with 'value'
'1 dozen' : '12', # I assume both 'key' and 'value' are strings
}
with open('test.csv', 'rb') as f:
reader = csv.reader(f) # pass the file to our csv reader
for row in reader: # iterate over the rows in the file
new_row = row # at first, just copy the row
for key, value in changes.items(): # iterate over 'changes' dictionary
new_row = [ x.replace(key, value) for x in new_row ] # make the substitutions
new_rows.append(new_row) # add the modified rows
with open('test.csv', 'wb') as f:
# Overwrite the old file with the modified rows
writer = csv.writer(f)
writer.writerows(new_rows)
यदि आप प्रोग्रामिंग और अजगर के लिए नए हैं सबसे trobulesome लाइन शायद
new_row = [ x.replace(key, value) for x in new_row ]
है, लेकिन यह सिर्फ एक सूची समझ है कि प्रभावी ढंग से
temp = []
for x in new_row:
temp.append(x.replace(key, value))
new_row = temp
के बराबर है है यह नहीं हो सकता त्रुटि का कारण बनें लेकिन कृपया देखें कि आप बी के बजाय बी झंडा यानी 'आरबी' के साथ खोलते हैं। csv.reader के लिए दस्तावेज़ कहते हैं, "अगर csvfile एक फ़ाइल ऑब्जेक्ट है, तो इसे प्लेटफॉर्म पर 'बी' ध्वज के साथ खोला जाना चाहिए जहां इससे कोई फर्क पड़ता है।" यह व्यापक रूप से ज्ञात है कि यह विंडोज पर एक फर्क पड़ता है। –