遍历CSV到特定行

时间:2019-06-26 19:43:51

标签: python-3.x

我有一个当前有效的脚本,该脚本当前正在CSV文件中的各个行之间进行迭代,但是我想将其配置为仅在一个范围(例如索引0-100)之间读取,但仍停留在如何正确完成此操作上。

这是我当前的代码:

for (name, subscriber) in zip(usernames, subscriber_ids):
 #do stuff with name / subscriber in selenium driver
    if len(elements) == 0:
        # logs information
    else:
        #logs information


with open('file', encoding='utf-8-sig') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    usernames = []
    subscriber_ids = []

    for row in readCSV:
        username = row[0]
        subscriberid = row[1]

        usernames.append(username)
        subscriber_ids.append(subscriberid)

我想做的只是从0-100行读取,或者从索引100-200开始,以此类推。我们将提供任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

with open("datafile") as myfile:
head = [next(myfile) for x in range(N)]
print(head)