循环遍历csv行并检查特定值

时间:2015-07-22 12:42:30

标签: python csv

您好我有一个关于循环的问题。现在的情况是我得到了一个csv文件,在那里我检查在column3(row [2])中是否有值" 1"存在。如果不是只是跳过它并再次循环添加值:

i = 1
maxuserid = 7255

result_liked = []
with open('source/to/file/user_id%i.csv' %i,'r') as fin:
    for row in csv.reader(fin, delimiter='\t'):
        if int(row[2]) >= 1:
            result_liked.append(row)
            i += 1
        else:
            i += 1
#more code

问题是我需要一个运行所有代码的for循环,并在运行完成后添加值" 1"直到我的 i 变量。

我的代码的目标是运行整个代码,在完成后我想要将值i从1加到2并再次运行循环,直到达到7255的maxuserid。如何从1到7255获得一个循环?

修改

import csv


maxuserid = 7255
result_liked = []    
for i in range(maxuserid):

    with open('source/to/file/user_id%i.csv' %(i+1),'r') as fin:
        for row in csv.reader(fin, delimiter='\t'):
            if int(row[2]) >= 1:
                result_liked.append(row)

    training_data = result_liked[:2]
    test_data = result_liked[2:]

    training_data_bookid = [el[1] for el in training_data]
    test_data_bookid = [el[1] for el in test_data]

    #training_data_bookid_int = map(int, training_data_bookid) #python2
    training_data_bookid_int = list(map(int, training_data_bookid)) #python3
    test_data_bookid_int = list(map(int, test_data_bookid)) #python3

    books_list = []
    for j in range(0,2):
        with open('source/to/file/output_new.csv', 'rt') as f:
            reader = csv.reader(f, delimiter=',', quotechar='"')
            for row in reader:
                get_book_id = training_data_bookid_int[j]
                if get_book_id == int(row[0]):
                    books_list.append([row[2],row[1]])

    b = sorted(books_list, reverse=True, key=lambda x:int(x[0]))
    c = [el[1] for el in b]

    c_int = list(map(int, c))

    check_training_vs_test = set(c_int) & set(test_data_bookid_int)        

    with open("result.txt", "a") as text_file:
        text_file.write("Userid: %i || Liked: %s || Test: %f" % (i, len(test_data), len(check_training_vs_test)))

1 个答案:

答案 0 :(得分:1)

尝试以下代码

maxuserid = 7255
result_liked = []
for i in range(maxuserid): # this loop iterates through all users files
    with open('source/to/file/user_id%d.csv' % (i+1),'r') as fin:
        for row in csv.reader(fin, delimiter='\t'):
            if int(row[2]) >= 1:
                result_liked.append(row)

<强>更新

我认为你需要这样的东西:

maxuserid = 7255
for i in range(maxuserid):
    result_liked = [] # form a separate list for each csv file
    with open('source/to/file/user_id%i.csv' %(i+1),'r') as fin:
        for row in csv.reader(fin, delimiter='\t'):
            if int(row[2]) >= 1:
                result_liked.append(row)

    if len(result_liked) < 3: # if list too few elements just go to next file
        continue
    training_data = result_liked[:2]
    test_data = result_liked[2:]
    ...