मैं फ़ाइल से फ़ील्ड नामों को कम करने के लिए csv.DictReader
चाहता हूं। The docsकहें "यदि फ़ील्डनाम पैरामीटर छोड़ा गया है, तो csvfile की पहली पंक्ति में मान फ़ील्डनाम के रूप में उपयोग किए जाएंगे।", लेकिन मेरे मामले में पहली पंक्ति में शीर्षक और दूसरी पंक्ति होती है जिसमें नाम होते हैं।csv.DictReader के साथ प्री हेडर लाइनों को कैसे छोड़ें?
मैं प्रति Python 3.2 skip a line in csv.DictReader रूप next(reader)
आवेदन कर सकते हैं नहीं है, क्योंकि FIELDNAME काम जगह जब पाठक आरंभ लेता है (या मैं गलत कर रहा हूँ)।
CanVec v1.1.0,,,,,,,,,^M
Entity,Attributes combination,"Specification Code
Point","Specification Code
Line","Specification Code
Area",Generic Code,Theme,"GML - Entity name
Shape - File name
Point","GML - Entity name
Shape - File name
Line","GML - Entity name
Shape - File name
Area"^M
Amusement park,Amusement park,,,2260012,2260009,LX,,,LX_2260009_2^M
Auto wrecker,Auto wrecker,,,2360012,2360009,IC,,,IC_2360009_2^M
मेरे कोड:
f = open(entities_table,'rb')
try:
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
reader = csv.DictReader(f, dialect=dialect)
print 'I think the field names are:\n%s\n' % (reader.fieldnames)
i = 0
for row in reader:
if i < 20:
print row
i = i + 1
finally:
f.close()
वर्तमान परिणाम:
I think the field names are:
['CanVec v1.1.0', '', '', '', '', '', '', '', '', '']
वांछित परिणाम:
I think the field names are:
['Entity','Attributes combination','"Specification Code Point"',...snip]
csvfile (Excel 2010, original source से निर्यात)
मुझे एहसास है कि यह पहली पंक्ति को हटाने और आगे बढ़ने के लिए उपयुक्त होगा, लेकिन मैं बस स्थिति में डेटा पढ़ने के करीब पहुंचने की कोशिश कर रहा हूं क्योंकि मैं मैन्युअल हस्तक्षेप को कम और कम कर सकता हूं।
यह एक अधिक लचीला समाधान है, बशर्ते एक निश्चित रूप से एक फ़ील्ड नाम (एक उचित उम्मीद) के साथ जानता हो। धन्यवाद। –