【问题标题】:Mapping list to csv using loops and dictionary使用循环和字典将列表映射到 csv
【发布时间】:2020-06-07 08:51:50
【问题描述】:

我正在使用 Python,我的 for 循环似乎有问题。

以下是我正在使用的数据集:

A) 我有这个 csv 文件,其中包含所有汽车数据

B) 我还有 csv 中的 Brand to Car(类型),如下所示

我需要构建另一个 CSV,将每一行分解为正确的品牌和汽车类型。同时确保它是准确的(例如有凯美瑞 L 和凯美瑞 LE - 有时它会同时拉动两者)。

这是我正在寻找的输出:

到目前为止,这是我的脚本:

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)Pull Title(例如黑色丰田凯美瑞 L)

2)从列表中提取汽车的品牌

3)从输出#2 - 映射到#B(图片中的csv文件)

这是我得到的(不幸的是):

我注意到的主要问题是:

1) 出于某种原因,Brand_Name 不会根据第二行或后续行而改变。所以它被困在了丰田。

2) car_type_output 拉出 Camry L 和 Camry LE

【问题讨论】:

    标签: python list csv dictionary for-loop


    【解决方案1】:

    问题:将每一行细分为正确的品牌、颜色和车型。

    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,,
    """
    

    1. 从文件A中拆分行的最简单方法。
      从文件B 中,只有带有品牌的第一行用于检测故障行:
      Honda Accord Navy
    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)
    
    1. 最昂贵的方法。这需要循环文件B中的dict 两次 文件A中的每行。
      检测乱序行:Honda Accord Navy,需要两个字符串比较。
    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)
    
    1. 数学方法,使用集合论
      car_type 的列表每行文件A 仅循环一次
      不需要检测乱序行的额外条件:Honda Accord Navy
      如果title items 中的setcar_type[x].set 中的superset,您将得到匹配。
    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'}
    

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 2021-04-30
      • 2011-01-13
      • 2016-08-31
      相关资源
      最近更新 更多