[英]Getting a unique value from csv file in python
I have following code in Python for twitter api, How do i iterate to get the values from CSV file. 我在Python中有以下用于twitter api的代码,如何迭代从CSV文件获取值。
get_tweet = set()
gettweet_list = []
with open(TWEET_FILE) as in_file:
for line in in_file:
gettweet_list.append(str(line))
get_tweet.update(set(gettweet_list))
del gettweet_list
if len(get_tweet) > 140:
get_tweet = get_tweet[:140]
return get_tweet
If you just want the first how just get the first row: 如果您只想要第一行,如何获得第一行:
with open(TWEET_FILE) as in_file:
data = set(next(in_file).split())
You also cannot index a set so get_tweet[:140]
would never work. 您也无法索引集合,因此
get_tweet[:140]
将永远无法工作。
If you want a set of all rows: 如果要一组所有行:
import csv
from itertools import chain
with open(TWEET_FILE) as in_file:
data = set(chain.from_iterable(csv.reader(in_file)))
Sets also have no order so even turning it back into a list and slicing will not give you the first row. 集合也没有顺序,因此即使将其返回到列表中,切片也不会给您第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.