使用DictRead从csv读取特定列

2024-05-20 01:52:35 发布

您现在位置:Python中文网/ 问答频道 /正文

从csv文件获取第二列值时遇到问题。在

以下代码将数据写入csv文件

def save_tweet_to_csv(ticker, tweet, emotion, confidence):
    file = Path(url_path + ticker + '_tweets.csv')
    if file.is_file():
        mode = 'a'
    else: 
        mode = 'w'
    with open(url_path + ticker + '_tweets.csv', mode, newline="\n", encoding="utf-8") as csvfile:
        fieldnames = ['tweet', 'emotion', 'confidence']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        if mode == 'w':
            writer.writeheader()
        writer.writerow({'tweet': tweet, 'emotion': emotion, 'confidence': confidence})

我试着用下面的方法从csv中获取情感

^{pr2}$

但我一直在想

File ....\Python36\lib\csv.py", line 87, in init self.reader = reader(f, dialect, *args, **kwds) TypeError: argument 1 must be an iterator

你知道这里出什么问题了吗?在


Tags: 文件csvcsvfilepathurlifmodetweets