基于python3.6
# -*-coding:utf-8 *- __author__ = 'lc_yy' from pykafka import KafkaClient import logging logging.basicConfig(level=logging.INFO) def putdata(filename,topic): client = KafkaClient(hosts='ip:port,ip:port') topic = client.topics[topic] producer = topic.get_producer() producer.start() with open(filename,'r',encoding='utf-8') as f: lines = f.readlines() count =0 for line in lines: count = count+1 line = line.strip() if not line: continue print("第{0}行:{1}".format(count,line)) producer.produce(str.encode(line)) print("一共写入{0}行。".format(count)) producer.stop() if __name__=="__main__": filename = r"C:/Users/Administrator/Desktop/xxxx.txt" topic = b"topicname" putdata(filename,topic)