【问题标题】:Output values from Python list into csv将 Python 列表中的值输出到 csv
【发布时间】:2019-11-19 19:10:44
【问题描述】:

我有一个函数,它以以下格式返回值:

["Stage 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium']", "Stage 2 : Package Description: The tablets are filled into box cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White", "Stage 3 : Package Description: The tablets are filled into box cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]

因此,某些阶段“颜色”将出现,而某些阶段“颜色”将不存在。我想将这些值提取到 csv 中,其中的列应如下所示:

CSV 格式的预期输出:

StageNumber  PackageDescription           Values1    Values2                        Values3       Colour
1.          Blisters are made in a ...    Blister    Foil                          Aluminium             

2.          The tablets are filled ...    Bottle   Cylindrically shaped Bottles     Polyethylene   White

到目前为止的代码:

paragraphs = ['The tablets are filled into cylindrically shaped bottles made of white coloured\npolyethylene. The volumes of the bottles depend on the tablet strength and amount of\ntablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured\npolypropylene and is equipped with a tamper proof ring.', 'PVC/PVDC blister pack', 'Blisters are made in a cold-forming process from an aluminium base web. Each tablet is\nfilled into a separate blister and a lidding foil of aluminium is welded on. The blisters\nare opened by pressing the tablets through the lidding foil.', '\n']

final_ref = [['Blister', 'Foil', 'Aluminium'], ['Blister', 'Base Web', 'PVC/PVDC'], ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'], ['Bottle', 'Screw Type Cap', 'Polypropylene'], ['Blister', 'Base Web', 'PVC'], ['Blister', 'Base Web', 'PVD/PVDC'], ['Bottle', 'Square Shaped Bottle', 'Polyethylene']]

colours = ['White', 'Yellow', 'Blue', 'Red', 'Green', 'Black', 'Brown', 'Silver', 'Purple', 'Navy blue', 'Gray', 'Orange', 'Maroon', 'pink', 'colourless', 'blue']

TEXT_WITHOUT_COLOUR = 'Stage {counter} : Package Description: {sen} Values: {values}'

TEXT_WITH_COLOUR = TEXT_WITHOUT_COLOUR + ' Colour: {colour}'

counter = 1
result = []


def is_missing(words, sen):
    for w in words:
        if w.lower() not in sen.lower():
            return True
    return False


for words in final_ref:
    for sen in paragraphs:
        if is_missing(words, sen):
            continue

        kwargs = {
            'counter': counter,
            'sen': sen,
            'values': str(words)
        }

        if words[0] == 'Bottle':
            for wd in colours:
                if wd.lower() in sen.lower():
                    kwargs['colour'] = wd
                    break
            text_const = TEXT_WITH_COLOUR
        else:
            text_const = TEXT_WITHOUT_COLOUR

        result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))
        counter += 1

print(result)

【问题讨论】:

    标签: python python-3.x list csv file-io


    【解决方案1】:

    我提出的解决方案不是最正确的,但它确实有效。您可以尝试改进它并使其适应您的需求。

    response = ["Stage 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil']", "Stage 2 : Package Description: The tablets are filled into box cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White", "Stage 3 : Package Description: The tablets are filled into box cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]
    
    
    def get_stage(word):
        return word.split("Stage ")[1].split(" : Package")[0]
    
    
    def get_description(word):
        return word.split("Package Description: ")[1].split(" Values: ")[0]
    
    
    def get_values(word):
        return eval(word.split("Values: ")[1].split(" Colour: ")[0])
    
    
    def concatenate_values(values, max_length):
        concatenated_values = ""
        for idx, value  in enumerate(range(max_length)):
            try:
                concatenated_values += values[idx] + ";"
            except:
                concatenated_values += ";"
    
        return concatenated_values
    
    def get_colour(word):
        colour = word.split(" Colour: ")
        if len(colour) == 1:
            return ""
        else:
            return colour[1]
    
    
    def create_header(max_length):
        values_header = ""
        for i in range(max_length):
            values_header += "Values" + str(i + 1) + ";"
    
        return "StageNumber;PackageDescription;" + values_header + "Colour"
    
    
    def get_length_values(response):
        max_length = 0
        for word in response:
            values = get_values(word)
            if len(values) > max_length:
                max_length = len(values)
    
        return max_length
    
    def get_formatted_csv_array(response):
        formatted_csv_array = []
        for id, word in enumerate(response):
            stage = get_stage(word)
            description = get_description(word)
            values = concatenate_values(get_values(word), get_length_values(response))
            colour = get_colour(word)
            formatted_csv_array.append(stage + ";" + description + ";" + values + colour)
    
        return formatted_csv_array
    
    
    file = open('output.csv', mode='w')
    file.write(create_header(get_length_values(response)) + "\n")
    for row in get_formatted_csv_array(response):
        file.write(row + "\n")
    file.close()
    

    希望对你有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-23
      • 2017-07-20
      • 2019-11-09
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多