Sunday 15 September 2013

python - Parse nested CSV columns into new CSV rows -


I have a CSV file that contains 3 columns of CSV data "Nested inside" I have a To create a new row, I want to divide the contents of those CSV columns, add the sequence number for each item (which corresponds to the sequence of items in the sequence within the CSV column), and then the corresponding ItemID number that is from the CSV column That matches it Area was. I look like Table 1 below and it looks like Table 2.

Table 1 (Original CSV file). The first row contains the column names:

  store id, date, store name, city, state, category 1CSV, category 2cv, category 3CSV 1051.2 / 16/2014, Easton, Columbus, Oh, "Flour, Yeast, Baking Powder", "Milk, Water, Oil", "Cinnamon, Sugar" 1425,1/14/2014, Crocker Park, Westlake, Oh, "Baking Powder , Yeast, four "," oil, milk, water " 

Table 2 (CSV column b D) The first row has column names:

  store id, date, store name, city, state, item name, item rank, item category 1051,2 / 16/2014, Easton, Columbus, OH, floor, 1,1 1051,2 / 16/2014, Easton, Columbus, OH, Yeast, 2,1 1051,2 / 16/2014, Easton, Columbus, OH, Baking Powder, 3,11051, 2/16/2014, Easton, Columbus, OH, Milk, 4,2, 1051,2 / 16/2014, Easton, Columbus, OH, Water, 5,2, 1051/2 / 16/2014 , Easton, Columbus, OH, Oil, 6,2, 1051,2 / 16/2014, Easton, Columbus, OH, Cinnamon, 7,3, 1051,2 / 16/2014, Easton, Columbus, OH, Sugar, 8,3 1425,1 / 14/2014, Crocker Park, Westlake, OH, Baking Powder, 1,1 1425,1/14/2014, Crocker Park, Westlake, OH, Yeast, 2,1 1425,1 / 14/2014, Crocker Park, Westlake, OH, Flour, 3,11425,1/14/2014, Crocker Park, Westlake, OH, Oil, 4,21425,1/14/2014, Crocker Park, Westlake, OH , Milk, 5,2 1425,1 / 14/2014, Crocker Park, Westlake, OH, Water, 6,2  

category 1CSV, Class 2 CV, and Category 3 CS label Le column 1 column item name table (example: dough),

  • The order of the item is item number
  • The most important aspect is that in the CSV list, and

  • The item is Category 1 or 2 or 3, depending on whether the data came from Category 1CSV, Category2CSV or Category 3CSV. Of these, the CSV is to maintain the order of objects from within the columns because they are divided into new lines. For example, in the StoreID 1051, the category "Flour, yeast, baking powder" has 1CSV content, they will map column names, item rank (sequence number), and item cadet, such as item name = dough, this item rank = 1 and Item Category = 1. This will be the first line in Table 2. In the second line, the item name will be = yeast, it is ItemRank = 2 and ItemCategory = 1, and does not end until you look like Table 2 above. In addition, you will see that the item rank numbering starts with the contents of category 1CSV, then the category 2 CV and finally the class 3 CSV is released.

    There are no items in all 3 CSV columns of all rows in the original CSV file. For example, in Store 1051 there were items in all 3 categories, but in Store 1425 there were only items in Category 1 and 2. Other stores can have items in only 1 category.

    My CSV file contains one two thousand lines. As a result the file may contain thousands of rows.

    How can I do this?

  • To indicate you in the right direction:

    • You are searching that the helper module of Python has a look at the docs for it - there are some good examples of running during the input file (any size and structure) and when converting to various python objects.
    • It's a pleasure, your 'empty line' object is a non-problem. The CSV module will recognize two commas (or whatever other delimiters you choose) as an empty string, as you would expect.

    As Daniel has indicated, I'm afraid I'm not completely clear that your worries are in the paragraphs of the clarification of your question, but I have full faith that if you If you play some time about the module, then you will hope that you can get it to work.


    No comments:

    Post a Comment