E
E
Error 5022018-10-31 14:58:36
Python
Error 502, 2018-10-31 14:58:36

Python script incorrectly looks for linked strings in CSV file, why?

The essence of this script is that it displays a list of values ​​from 2 files corresponding to the id value from the first file. As a result, it outputs a list only for the very first id, and for the rest it outputs an empty list "[ ]". Moreover, for each of these id there are corresponding values, empty lists should not be displayed there. What am I doing wrong?

posts_table = open("posts.csv")
cat_rel_table = open("category_relationship.csv")

reader_1 = csv.DictReader(posts_table)
reader_2 = csv.DictReader(cat_rel_table)

def cat_name(post_id):
    cat_list = []
    for cat in reader_2:
        if cat['post_id'] == post_id:
            cat_list.append(cat['cat_id'])
    return cat_list

for post in reader_1:
    if (post['type'] == "8" and post['status'] == "1"):
        print(post['id'], cat_name(post['id']))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pugachev, 2018-10-31
@NullByte

Error 502 ,
After iterating over reader_2 do
or before iteration,
you get the idea

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question