所以我收集数据并将这些数据保存到csv文件中,但是出于演示目的,我想根据它的相关“顺序”重新排序每个csv文件中的列。
我使用这个问题(write CSV columns out in a different order in Python)作为指导,但我不确定为什么我会收到错误
writeindices = [name2index[name] for name in writenames]
KeyError: % Processor Time
我跑的时候。请注意,此错误似乎不仅限于字符串% Processor Time'
。
我哪里错了?
这是我的代码:
CPU_order=["%"+" Processor Time", "%"+" User Time", "Other"]
Memory_order=["Available Bytes", "Pages/sec", "Pages Output/sec", "Pages Input/sec", "Page Faults/sec"]
def reorder_csv(path,title,input_file):
if title == 'CPU':
order=CPU_order
elif title == 'Memory':
order=Memory_order
output_file=path+'/'+title+'_reorder'+'.csv'
writenames = order
reader = csv.reader(input_file)
writer = csv.writer(open(output_file, 'wb'))
readnames = reader.next()
name2index = dict((name, index) for index, name in enumerate(readnames))
writeindices = [name2index[name] for name in writenames]
reorderfunc = operator.itemgetter(*writeindices)
writer.writerow(writenames)
for row in reader:
writer.writerow(reorderfunc(row))
以下是输入CSV文件的示例:
,CPU\% User Time,CPU\% Processor Time,CPU\Other
05/23/2016 06:01:51.552,0,0,0
05/23/2016 06:02:01.567,0.038940741537158409,0.62259056657940626,0.077882481554869071
05/23/2016 06:02:11.566,0.03900149141703179,0.77956981074955856,0
05/23/2016 06:02:21.566,0,0,0
05/23/2016 06:02:31.566,0,1.1695867249963632,0
答案 0 :(得分:1)
您的代码有效。您的数据没有名为“%Processor Time ”的列。以下是我使用的示例数据:
Other,% User Time,% Processor Time
o1,u1,p1
o2,u2,p2
以下是我所说的代码:
reorder_csv('.', 'CPU', open('data.csv'))
使用这些设置,一切正常。请检查您的数据。
现在我看到了您的数据,看起来您的列名称如“CPU \%Processor Time ”并希望将其转换为“%Processor Time “在写出之前。您需要做的就是以这种方式创建name2index
:
name2index = dict((name.replace('CPU\\', ''), index) for index, name in enumerate(readnames))
这里的区别在于name
而不是name.replace('CPU\\', '')
,你应该csv.DictReader
,它摆脱了 CPU \ 部分。
我将您的代码重新设计为使用csv.DictWriter
和transformer
。我还假设“ CPU \%Prvileged Time ”将转换为“其他”。如果不是这种情况,您可以在import csv
import os
def rename_columns(row):
""" Take a row (dictionary) of data and return a new row with columns renamed """
transformer = {
'CPU\\% User Time': '% User Time',
'CPU\\% Processor Time': '% Processor Time',
'CPU\\% Privileged Time': 'Other',
}
new_row = {transformer.get(k, k): v for k, v in row.items()}
return new_row
def reorder_csv(path, title, input_file):
header = dict(
CPU=["% Processor Time", "% User Time", "Other"],
Memory=["Available Bytes", "Pages/sec", "Pages Output/sec", "Pages Input/sec", "Page Faults/sec"],
)
reader = csv.DictReader(input_file)
output_filename = os.path.join(path, '{}_reorder2.csv'.format(title))
with open(output_filename, 'wb') as outfile:
# Create a new writer where each row is a dictionary.
# If the row contains extra keys, ignore them
writer = csv.DictWriter(outfile, header[title], extrasaction='ignore')
writer.writeheader()
for row in reader:
# Each row is a dictionary, not list
print row
row = rename_columns(row)
print row
print
writer.writerow(row)
词典中修复它。
StudentRec.birthdate = fields[i];
string[] format = { "MM/dd/yy" };
DateTime dateValue;
if (StudentRec.birthdate.Length > 0)
{
if (!DateTime.TryParseExact(StudentRec.birthdate, format,
new CultureInfo("en-US"),
DateTimeStyles.AllowWhiteSpaces ,
out dateValue))
{
Console.WriteLine(StudentRec.birthdate);
throw new MyException("Birthdate is not valid ", o, strCNFileErr);
}
}