refactor: load csv file with csv.DictReader

This commit is contained in:
debugtalk
2018-03-22 21:28:46 +08:00
parent 0dff8587fa
commit 1c44ae764b
2 changed files with 6 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
__title__ = 'HttpRunner'
__description__ = 'One-stop solution for HTTP(S) testing.'
__url__ = 'https://github.com/HttpRunner/HttpRunner'
__version__ = '1.3.1'
__version__ = '1.3.2'
__author__ = 'debugtalk'
__author_email__ = 'mail@debugtalk.com'
__license__ = 'MIT'

View File

@@ -1,4 +1,5 @@
import ast
import csv
import io
import itertools
import json
@@ -62,33 +63,11 @@ def _load_csv_file(csv_file):
]
"""
csv_content_list = []
parameter_list = None
collums_num = 0
with io.open(csv_file, encoding='utf-8') as data_file:
for line in data_file:
line_data = line.strip().split(",")
if line_data == [""]:
# ignore empty line
continue
if not parameter_list:
# first line will always be parameter name
parameter_list = line_data
collums_num = len(parameter_list)
continue
# from the second line
if len(line_data) != collums_num:
err_msg = "CSV file collums does match with headers.\n"
err_msg += "\tcsv file path: {}\n".format(csv_file)
err_msg += "\terror line content: {}".format(line_data)
raise exception.FileFormatError(err_msg)
else:
data = {}
for index, parameter_name in enumerate(parameter_list):
data[parameter_name] = line_data[index]
csv_content_list.append(data)
with io.open(csv_file, encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
csv_content_list.append(row)
return csv_content_list