python导入csv文件是如何预览后10行-使用python中的csv reader只读取前N行csv文件

我将一些csv文件的第二列中包含的文本添加到一个列表中,以便稍后对列表中的每个项目执行情绪分析。我的代码目前完全适用于大型csv文件,但是我对列表中的项目执行的情感分析花费了太长时间,这就是为什么我只想读取每个csv文件的前200行。代码如下所示: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行?在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值