python处理csv中的空值方法

yipeiwu_com5年前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通过zabbix api获取主机

zabbix强大地方在于有强大的api,zabbix 的api可以拿到zabbix大部分数据,目前我所需的数据基本可以通过api获取,以下是通过zabbix api获取的主机信息pyth...

Python使用微信SDK实现的微信支付功能示例

本文实例讲述了Python使用微信SDK实现的微信支付功能。分享给大家供大家参考,具体如下: 最近一段时间一直在搞微信平台开发,v3.37版本微信支付接口变化贼大,所以就看着php的de...

Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

在python的Beautiful Soup 4 扩展库的使用过程中出现了 TypeError: list indices must be integers or slices, no...

视频合并时使用python批量修改文件名的方法

视频合并时使用python批量修改文件名的方法

不知道大家有没有遇到这样的情况,比如视频合并时文件名没有按照正常顺序排列,像这样    可见,文件名排序是乱的。这个样子合并出来的视频一定也是乱的。所以得想办法把文件...

python flask web服务实现更换默认端口和IP的方法

flask web后台启动后会发现默认是 localhost 127.0.0.1:5000 如果需要修改,方便调试发布 可以采用以下方式运行 from flask import Fl...