python处理csv中的空值方法

yipeiwu_com6年前Python基础

如下所示:

# -*- 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中的空值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python+opencv实现动态物体识别

python+opencv实现动态物体识别

注意:这种方法十分受光线变化影响 自己在家拿着手机瞎晃的成果图: 源代码: # -*- coding: utf-8 -*- """ Created on Wed Sep 2...

如何高效使用Python字典的方法详解

前言 众所周知字典(dict)对象是 Python 最常用的数据结构,社区曾有人开玩笑地说:"Python企图用字典装载整个世界",字典在Python中的重要性不言而喻,这里整理了几个关...

Python实现确认字符串是否包含指定字符串的实例

有时候我们需要在某段字符串或者某段语句中去查找确认是否包含我们所需要的字符串信息, 举例子说、 某段变量是:A= ”My name is Clay, and you can get my...

解决Python 中英文混输格式对齐的问题

Python中使用str.format进行格式化输出 format使用方法较多,这里只说明其在填充与对齐上的使用方法: 填充与对齐 填充常跟对齐一起使用 ^、<、>分别是居中...

浅谈Python数据类型判断及列表脚本操作

浅谈Python数据类型判断及列表脚本操作

数据类型判断 在python(版本3.0以上)使用变量,并进行值比较时。有时候会出现以下错误: TypeError: unorderable types: NoneType() <...