本文整理汇总了Python中csv.DictReader.fieldnames方法的典型用法代码示例。如果您正苦于以下问题:Python DictReader.fieldnames方法的具体用法?Python DictReader.fieldnames怎么用?Python DictReader.fieldnames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类csv.DictReader
的用法示例。
在下文中一共展示了DictReader.fieldnames方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_data
# 需要导入模块: from csv import DictReader [as 别名]
# 或者: from csv.DictReader import fieldnames [as 别名]
def load_data(uri, dateFormat):
logging.info('loading data; uri: {0}'.format(uri))
from urllib2 import urlopen
from csv import DictReader
reader = DictReader(urlopen(uri).readlines())
encodedFieldNames = []
for fieldname in reader.fieldnames:
encodedFieldNames.append(fieldname.decode("utf-8-sig").encode("utf-8"))
reader.fieldnames = encodedFieldNames
data = []
from time import strptime
for row in reader:
data.append({
'date': strptime(row['Date'], dateFormat),
'open': float(row['Open']),
'close': float(row['Close']),
'high': float(row['High']),
'low': float(row['Low']),
'volume': float(row['Volume'])
})
return data
示例2: csvInput
# 需要导入模块: from csv import DictReader [as 别名]
# 或者: from csv.DictReader import fieldnames [as 别名]
def csvInput(file,options,dialect='excel'):
header=options['header']
from csv import DictReader
with open(file,'r') as f:
if not header:
reader = DictReader(f,dialect=dialect)
else:
reader = DictReader(f,dialect=dialect,fieldnames=header.split(','))
reader.fieldnames = map(options['alias'],reader.fieldnames)
entries =[line for line in reader]
map(lambda(dict):
dict.update({"file":file,
"format":fileType(file)}),
entries)
return entries
示例3: walk
# 需要导入模块: from csv import DictReader [as 别名]
# 或者: from csv.DictReader import fieldnames [as 别名]
for dirpath, dirnames, filenames in walk(root):
if match(".*\/mvol\/\d{4}\/\d{4}\/\d{4}", dirpath):
print("Stopping recursion at "+dirpath)
del dirnames[:]
for filename in filenames:
print("Scanning " + filename)
if match("mvol-\d{4}-\d{4}-\d{4}\.struct\.txt$", filename) or \
match("mvol-\d{4}-\d{4}-\d{4}\.txt$", filename):
print("Acting on " + filename)
try:
with open(join(dirpath, filename), 'r') as f:
reader = DictReader(f, delimiter='\t')
assert('object' in reader.fieldnames or
'Object' in reader.fieldnames)
if reader.fieldnames != [x.lower() for x in reader.fieldnames]:
print("Editing " + join(dirpath, filename))
reader.fieldnames = [x.lower() for x in reader.fieldnames]
with open(join(dirpath, filename)+".new",'w') as out:
writer = DictWriter(out, fieldnames=reader.fieldnames, delimiter='\t',quoting=QUOTE_MINIMAL)
writer.writeheader()
for row in reader:
rowDict = {}
for header in reader.fieldnames:
rowDict[header] = row[header]
writer.writerow(rowDict)
move(join(dirpath, filename), join(dirpath, filename+'.old'))
assert(isfile(join(dirpath, filename)) == False)
move(join(dirpath, filename+'.new'), join(dirpath, filename))
except Exception as e:
print(e)