仅使用python中的csv阅读器读取csv文件的前N行 [英] only reading first N rows of csv file with csv reader in python

查看:52
本文介绍了仅使用python中的csv阅读器读取csv文件的前N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将多个csv文件的第二列中包含的文本添加到一个列表中,以便以后对列表中的每个项目执行情感分析.目前,我的代码已完全适用于大型csv文件,但是我对列表中的项目进行的情感分析花费的时间太长,这就是为什么我只想读取每个csv文件的前200行的原因.代码如下:

I'm adding the text contained in the second column of a number of csv files into one list to later perform sentiment analysis on each item in the list. My code is fully working for large csv files at the moment, but the sentiment analysis I'm performing on the items in the list takes too long which is why I want to only read the first 200 rows per csv file. The code looks as follows:

import nltk, string, lumpy 
import math
import glob
from collections import defaultdict
columns = defaultdict(list)
from nltk.corpus import stopwords
import math
import sentiment_mod as s
import glob

lijst = glob.glob('21cf/*.csv')

tweets1 = []
for item in lijst:
    stopwords_set = set(stopwords.words("english"))
    with open(item, encoding = 'latin-1') as d:
        reader1=csv.reader(d)
        next(reader1)
        for row in reader1:
            tweets1.extend([row[2]])
        words_cleaned = [" ".join([words for words in sentence.split() if 'http' not in words and not words.startswith('@')]) for sentence in tweets1]
        words_filtered = [e.lower() for e in words_cleaned]
        words_without_stopwords = [word for word in words_filtered if not word in stopwords_set]
    tweets1 = words_without_stopwords
    tweets1 = list(filter(None, tweets1))

如何确保仅使用csv阅读器读取每个csv文件的前200行?

How do I make sure to only read over the first 200 rows per csv file with the csv reader?

推荐答案

最简短,最惯用的方法可能是使用

The shortest and most idiomatic way is probably to use itertools.islice:

import itertools
...
        for row in itertools.islice(reader1, 200):
            ...

这篇关于仅使用python中的csv阅读器读取csv文件的前N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆