匹配来自单独CSV文件的特定值并连接行

时间:2018-07-12 09:59:42

标签: python python-3.x csv

我正在尝试创建一个查看2个CSV文件的python v3.x程序。
最初,我需要将CAR CSV列表分为两个列表,“昂贵的汽车”和“廉价的汽车”,并为每个列表分配相关的行。

然后我打算从指标CSV中获取一个指标,看看它是针对昂贵汽车,廉价车还是两者的指标,然后将该指标CSV行中的值与匹配的行连接起来在汽车CSV中。

  
      
  1. CAR CSV-列出许多汽车的详细信息

         

    “汽车代码”,“价格”,“别名”,“组”
      “ Car1”,“ 100”,“ Blue Car”,“ Cheap Cars”
      “ Car2”,“ 900”,“金车”,“昂贵的汽车”
      “ Car3”,“ 150”,“ Red Car”,“ Cheap Cars”
      “ Car4”,“ 999”,“白金车”,“昂贵的车”
      “ Car5”,“ 122”,“棕色汽车”,“便宜的汽车”
      “ Car6”,“ 500”,“粉红色汽车”,“便宜的汽车”,“昂贵的汽车”

  2.   
  3. 指示器CSV-列出汽车的可能故障/错误

         

    “ Indicator_Field”,“ Indicator_Value”,“ Desc”,“ Groups”
      “故障”,“生锈”,“车身生锈”,“便宜的汽车;昂贵的汽车”
      “错误”,“窗户绕线器已停止”,“手动窗户绕线器已损坏”,“廉价汽车”
      “故障”,“ V12问题”,“ V12发动机问题”,“昂贵的汽车”

  4.   
  5. 连接示例

         

    {“ code”:“ Car1”,“ Error”:“ Rusting”}
      {“代码”:“ Car2”,“故障”:“ V12问题”}
      {“ code”:“ Car5”,“ Error”:“ Window Winder Stopped”}
      {“ code”:“ Car1”,“ Error”:“ Window Winder Stopped”}

  6.   

到目前为止,我所能做的就是从每个CSV中选择一条随机行,并从每个CSV中连接特定的值。问题是现在所有指标和汽车类别都匹配。

from sys import argv
from time import sleep
import random
import csv
import heapq

script, indicator_file, car_file = argv
ind = ''
car = ''

def indicatorDefinition(i):
    with open(i) as file:
        reader = csv.DictReader(file)
        random_line, = heapq.nlargest(1, reader, key=lambda L: random.random())
        global ind
        ind = random_line['Indicator_Field']+'":"'+random_line['Indicator_Value']+'"'

def carDefinition(n):
    with open(n) as file:
        reader = csv.DictReader(file)
        random_line, = heapq.nlargest(1, reader, key=lambda L: random.random())
        global net
        net = '"code":"'+random_line['Car code']+'","'




def counter():
    count = 0
    while count < 6:
        carDefinition(car_file)
        indicatorDefinition(indicator_file)
        print("{"+car+ind+"}")
        sleep(random.randint(1,10))
        count += 1


counter()

1 个答案:

答案 0 :(得分:0)

也许是这样的事情? 我认为“组”字段应该拆分并且不能按原样使用。根据注释进行了更新以读取所有组。

import csv
import io
import random
import collections
import itertools

# Data included in source for clarity

car_csv = """
"Car code","Price","Alias","Groups"
"Car1","100","Blue Car","Cheap Cars"
"Car2","900","Gold Car","Expensive Cars"
"Car3","150","Red Car","Cheap Cars"
"Car4","999","Platinum Car","Expensive Cars"
"Car5","122","Brown Car","Cheap Cars"
"Car6","500","Pink Car","Cheap Cars","Expensive Cars"
"TestCarWithThreeGroups","500","Pink Car","Cheap Cars","Expensive Cars","Broken Cars"
""".lstrip()

indicator_csv = """
"Indicator_Field","Indicator_Value","Desc","Groups"
"Fault","Rusting","Bodywork is rusting","Cheap Cars; Expensive Cars"
"Error","Window Winder Stopped","Manual window winder broken","Cheap Cars"
"Fault","V12 Issues","V12 Engine Problems","Expensive Cars"
""".lstrip()

# Read data (replace io.StringIO(...) with a file object to read from a file)
indicator_data = list(csv.DictReader(io.StringIO(indicator_csv)))

# Note `restkey` to capture all of the additional columns into `OtherGroups`.
car_data = list(csv.DictReader(io.StringIO(car_csv), restkey='OtherGroups'))

# Pre-massage the car data so `Groups` is always a set of groups, and `OtherGroups` is no longer there:
for car in car_data:
    car['Groups'] = {car['Groups']} | set(car.pop('OtherGroups', ()))


# Create a mapping of groups <-> indicators
indicators_by_group = collections.defaultdict(list)
for indicator in indicator_data:
    for group in indicator['Groups'].split('; '):
        indicators_by_group[group].append(indicator)


def generate_car():
    car = random.choice(car_data)
    # Concatenate all indicators based on the groups of the car
    available_indicators = list(itertools.chain(
        *(indicators_by_group.get(group, []) for group in car['Groups'])
    ))
    # Choose a random indicator -- this will crash if there are no available indicators
    # for the car.
    indicator = random.choice(available_indicators)
    # Generate output datum
    return {
        'code': car['Car code'],
        indicator['Indicator_Field']: indicator['Indicator_Value'],
    }


for x in range(10):
    print(generate_car())

示例输出:

{'code': 'Car1', 'Error': 'Window Winder Stopped'}
{'code': 'Car6', 'Error': 'Window Winder Stopped'}
{'code': 'Car2', 'Fault': 'Rusting'}
{'code': 'Car6', 'Error': 'Window Winder Stopped'}
{'code': 'TestCarWithThreeGroups', 'Error': 'Window Winder Stopped'}
{'code': 'Car5', 'Fault': 'Rusting'}
{'code': 'Car2', 'Fault': 'Rusting'}
{'code': 'TestCarWithThreeGroups', 'Fault': 'V12 Issues'}
{'code': 'Car6', 'Fault': 'Rusting'}
{'code': 'TestCarWithThreeGroups', 'Error': 'Window Winder Stopped'}