python - 使用数组将 csv 转换为类别子类别

标签 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

最佳答案

如果您读取 csv 行并访问数据,那么您以后就可以按照您想要的方式进行操作。

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

输出:

Categories relations:
  New Products
  New Products \ Best Sellers
  New Products \ Discounted Products & Special Offers
  New Products \ Best Sellers \ Previous Months
  New Products \ Best Sellers \ Last Month
  New Products \ Best Sellers \ New this Month

关于python - 使用数组将 csv 转换为类别子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599734/

相关文章:

Javascript:如何在多维数组上实现迭代器?

php - APL 中的爆炸和内爆

categories - 如何在opencart中显示从子类别到父类别的产品?

cocoa - cocoa 中的类别和单例

Java JFreeChart 类别步骤图表水平(图像解释)

python - 在Python中复制值集的列表

python - 如何更改某些代码语句的名称

arrays - 获取通过数组中的 http 调用接收到的 json - Swift iOS

python - python 搜索中出现'builtin_function_or_method'对象不可迭代'错误?

python - Dict.setdefault 插入排序到列表中