算法步骤
- 打开csv文件
- 读取文件头
- 读取剩余行
- 发生错误的时候抛出异常
代码(python)
import csv
csvFileName = "input.csv"
try:
with open(csvFileName) as f:
reader = csv.reader(f)
head = reader.next()
data = [row for row in reader]
except csv.Error as e:
print "Error reading CSV file at line %s: %s" % (reader.line_num, e)
sys.exit(-1)
# 输出csv文件头
if header:
print header
print "-------------"
#输出csv数据
for datarow in data:
print datarow