Python 使用循环和字典将列表映射到csv
我正在使用Python,似乎我的for循环有问题 以下是我正在使用的数据集: A)我有一个csv文件,其中包含所有汽车数据 B)我还有csv中的品牌到汽车(类型),如下所示 我需要建立的是建立另一个CSV,将每条生产线分解为正确的品牌和车型。同时确保它是准确的(例如,有凯美瑞L和凯美瑞LE-有时它拉两个) 这是我正在寻找的输出: 到目前为止,我的脚本如下:Python 使用循环和字典将列表映射到csv,python,list,csv,dictionary,for-loop,Python,List,Csv,Dictionary,For Loop,我正在使用Python,似乎我的for循环有问题 以下是我正在使用的数据集: A)我有一个csv文件,其中包含所有汽车数据 B)我还有csv中的品牌到汽车(类型),如下所示 我需要建立的是建立另一个CSV,将每条生产线分解为正确的品牌和车型。同时确保它是准确的(例如,有凯美瑞L和凯美瑞LE-有时它拉两个) 这是我正在寻找的输出: 到目前为止,我的脚本如下: temp_car_list = [] reader_type = csv.DictReader(type_library) with
temp_car_list = []
reader_type = csv.DictReader(type_library)
with open(file_name) as Output_file:
reader = csv.DictReader(Output_file)
for row in reader:
Title = row ["Item Name"]
print (Title)
for brand in brand_library_iterate:
if brand in Title:
Brand_Name = brand
print(Brand_Name)
#Get Type Library List
for Car_type in reader_type:
Brand_String = str(Brand_Name)
print(Brand_String)
type_list = temp_car_list.append(Car_type[Brand_String])
print(temp_car_list)
for car_type in temp_car_list:
if car_type in Title:
car_type_output = car_type
print (car_type_output)
temp_car_list.clear()
脚本的逻辑:
1) 拉动标题(例如黑色丰田凯美瑞L)
2) 从列表中提取汽车的品牌
3) 从输出#2-映射到#B(图片中的csv文件)
以下是我得到的(不幸):
我注意到的主要问题是:
1) 由于某些原因,品牌名称不会随着第二行或后续行的变化而变化。因此,它被困在丰田
2) car_type_输出同时拉出凯美瑞L和凯美瑞LE
问题:将每一行细分为正确的品牌、颜色和车型
A
中拆分行从文件
B
中,只有带有品牌的第一行用于检测故障行:本田雅阁海军
car\u type
列表仅在文件A的每行循环一次
检测故障行的附加条件:Honda Accord Navy
,不是必需的。
如果标题项的集合
是车型[x]的超集
,则会得到一个匹配项。集合
A
中拆分行从文件
B
中,只有带有品牌的第一行用于检测故障行:本田雅阁海军
car\u type
列表仅在文件A的每行循环一次
检测故障行的附加条件:Honda Accord Navy
,不是必需的。
如果标题项的集合
是车型[x]的超集
,则会得到一个匹配项。集合
A = """Item Name
Black Toyota Camry L
Honda Accord Navy
Grey Toyota Corolla
Black Nissan Murano
Silver Toyota Camry LE
"""
B = """Toyota,Nissan,Honda
Camry L,Murano,Accord
Corolla,Rogue,Civic
Avalon,Pathfinder,CR-V
Highlander,Maxima,HR-V
Prius,Altima,
Camry LE,,
"""
import io
def simple_split_title():
# with open(<name of your file B>) as fh:
with io.StringIO(B) as fh:
brands = fh.readline().rstrip().split(',')
# with open(<name of your file A>) as fh:
with io.StringIO(A) as fh:
_ = next(fh)
for line in fh:
title = line.rstrip()
item = title.split(' ')
if item[0] in brands:
_color, _brand, _type, _last = len(item) - 1, 0, 1, len(item) - 1
else:
_color, _brand, _type, _last = 0, 1, 2, len(item)
result = {'title': title,
'brand': item[_brand],
'color': item[_color],
'type': ' '.join(item[_type:_last])}
print(result)
import io, csv
def looping_dict():
# with open(<name of your file B>) as fh:
with io.StringIO(B) as fh:
car_type = [_dict for _dict in csv.DictReader(fh)]
# with open(<name of your file A>) as fh:
with io.StringIO(A) as fh:
_ = next(fh)
for line in fh:
title = line.rstrip()
result = {'title': title, 'brand': '', 'color': '', 'type': ''}
# Get brand
for brand in car_type[0].keys():
if brand in title:
result['brand'] = brand
title = title.replace(brand + ' ', '')
break
# Get type
for _type in car_type:
if title.endswith(_type[brand]) or title.startswith(_type[brand]):
result['type'] = _type[brand]
title = title.replace(_type[brand], '')
break
# Get color
result['color'] = title.strip()
print(result)
import io, csv
from collections import namedtuple
def theory_of_sets():
CarType = namedtuple('CarType', 'set brand type')
car_type = []
# with open(<name of your file B>) as fh:
with io.StringIO(B) as fh:
for _dict in csv.DictReader(fh):
for brand, _type in _dict.items():
_set = {brand} | set(_type.split(' '))
car_type.append(CarType._make((_set, brand, _type)))
# with open(<name of your file A>) as fh:
with io.StringIO(A) as fh:
_ = next(fh)
for line in fh:
title = line.rstrip()
_title = title.split(' ')
_items = set(_title)
result = None
for ct in car_type:
if _items.issuperset(ct.set):
result = {'title': title,
'brand': ct.brand,
'color': (_items - ct.set).pop(),
'type': ct.type}
break
print(result)
{'title': 'Black Toyota Camry L', 'brand': 'Toyota', 'color': 'Black', 'type': 'Camry L'}
{'title': 'Honda Accord Navy', 'brand': 'Honda', 'color': 'Navy', 'type': 'Accord'}
{'title': 'Grey Toyota Corolla', 'brand': 'Toyota', 'color': 'Grey', 'type': 'Corolla'}
{'title': 'Black Nissan Murano', 'brand': 'Nissan', 'color': 'Black', 'type': 'Murano'}
{'title': 'Silver Toyota Camry LE', 'brand': 'Toyota', 'color': 'Silver', 'type': 'Camry LE'}