使用数组将csv转换为category-subcategory

时间:2016-02-24 10:38:14

标签: python arrays categories

input table is here

上面是我在csv中的输入表     我试图在python中使用数组和while循环。我是这门语言的新手。循环应该出现两次给Category \ sub-category \ sub-category_1顺序...我试图使用split()。输出应该如下所示

 import csv
    with open('D:\\test.csv', 'rb') as f:
        reader = csv.reader(f, delimiter='',quotechar='|') 
        data = [] 
    for name in reader:
        data[name] = []

output should be like this

1 个答案:

答案 0 :(得分:1)

如果您阅读csv的行并访问数据,那么您可以稍后操作您想要的方式。

<header class="mainHeader">
  <img src="img/css.jpg" />
  <nav>
    <ul>
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Porfolio</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>

输出:

cats = {}

with open('my.csv', "r") as ins:
    # check each line of the fine
    for line in ins:
        # remove double quotes: replace('"', '')
        # remove break line : rstrip()
        a = str(line).replace('"', '').rstrip().split('|')
        if a[0] != 'CatNo':
            cats[int(a[0])] = a[1:];

for p in cats:
    print 'cat_id: %d, value: %s' % (p, cats[p])

# you can access the value by the int ID
print cats[1001]

更新了您的问题的脚本:

cat_id: 100, value: ['Best Sellers', 'Best Sellers']
cat_id: 1001, value: ['New this Month', 'New Products\\New this Month']
cat_id: 10, value: ['New Products', 'New Products']
cat_id: 1003, value: ['Previous Months', 'New Products\\Previous Months']
cat_id: 110, value: ['Promotional Material', 'Promotional Material']
cat_id: 120, value: ['Discounted Products & Special Offers', 'Discounted Products & Special Offers']
cat_id: 1002, value: ['Last Month', 'New Products\\Last Month']
['New this Month', 'New Products\\New this Month']

输出:

categories = {}

def get_parent_category(cat_id):
    if len(cat_id) <= 2:
        return '';
    else:
        return cat_id[:-1]

with open('my.csv', "r") as ins:
    for line in ins:
        # remove double quotes: replace('"', '')
        # remove break line : rstrip()
        a = str(line).replace('"', '').rstrip().split('|')

        cat_id = a[0]

        if cat_id != 'CatNo':
            categories[cat_id] = {
                'parent': get_parent_category(cat_id),
                'desc': a[1],
                'long_desc': a[2]
            };

print 'Categories relations:'

for p in categories:

    parent = categories[p]['parent']
    output = categories[p]['desc']

    while parent != '':
        output = categories[parent]['desc'] + ' \\ ' + output
        parent = categories[parent]['parent']

    print '\t', output