DDR爱好者之家 Design By 杰米

如下所示:

# -*- coding: UTF-8 -*-
import jieba.posseg
import tensorflow as tf
import pandas as pd
import csv
import math
"""
1.必須獲取CSV文件夾(ID:文本)
2.返回(ID:分词后的文本)
"""
flags = tf.app.flags
flags.DEFINE_string("train_file_address","D:/NLPWORD/cut_word_test/hzytest.csv","添加训练数据文件")
flags.DEFINE_string("result_file_address","D:/NLPWORD/cut_word_test/hzytest_result.csv","生成结果数据文件")
FLAGS = tf.app.flags.FLAGS
def cut_word(train_data):
 """
 把数据按照行进行遍历,然后把结果按照行写在csv中
 :return:分词结果list
 """
 jieba.load_userdict("newdict.txt")
 with open(FLAGS.result_file_address, "w", encoding='utf8') as csvfile:
 writer = csv.writer(csvfile)
 for row in train_data.index:
  datas = train_data.loc[row].values[1]
  if isinstance(datas,str) or not math.isnan(datas):
  words = jieba.posseg.cut(datas)
  line = ''
  for word in words:
   line = line + word.word + " "
  writer.writerow([train_data.loc[row].values[0], line])
def main(_):
 data = pd.read_csv(FLAGS.train_file_address)
 cut_word(data)

if __name__ == "__main__":
 tf.app.run(main)

以上这篇python处理csv中的空值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米