php逐行读取.txt文件内容,并解析每行内容
// 读取nlp text 并存到mongodb
public function readNLP(&$errorCode,&$errorMessage)
{
try{
// $_SERVER["DOCUMENT_ROOT"],获取当前运行脚本所在文档根目录。$filePath为.txt文件所在路径
$filePath = $_SERVER["DOCUMENT_ROOT"] . "/checkdata/app/files/nlp.txt";
$file = fopen($filePath, "r"); // 以只读的方式打开文件
if(empty($file)){
$errorCode = 201;
$errorMessage = "file not found";
return;
}
$i = 0;
//输出文本中所有的行,直到文件结束为止。
while(!feof($file)) {
$itemStr = fgets($file); //fgets()函数从文件指针中读取一行
$itemArray = explode("\t",$itemStr); // 将tab分割的各部分内容提取出来
$itemArray = array_filter($itemArray); // 对itemArray进行校验
$textDB = new Text();
if($textDB->findItemByText($itemArray[3]) === false){ // 数据库中不存在该item,insert
$addResult = $textDB->addNewItem($itemArray[3],$itemArray[4]);
if($addResult === false){
$errorCode = 201;
$errorMessage = "insert new item failed";
return "currentIndex: " . $i . " , " . $itemArray[3];
}
}
++$i;
}
fclose($file);
}catch (Exception $exception){
$errorCode = $exception->getCode();
$errorMessage = $exception->getMessage();
}
return true;
}
php逐行读取.txt文件内容,并解析每行内容的更多相关文章
- 别再用"while (!feof(file))"来逐行读取txt文件了!
起因 执行一个C/C++程序出现segment fault.它逐行读取文本文件,每一行是一个图片名字,然后读图.处理图像,etc. 发现最后一次读取的文件名不存在(空的). 正确的逐行读取txt文件 ...
- python 逐行读取txt文件
逐行读取txt文件 path = r'D:\123456\1.txt'with open(path, 'r', encoding='utf-8') as f: for line in f: ...
- 3.C++逐行读取txt文件数据,利用getline -windows编程
引言:今天学会了getline的用法,顺手编写一个逐行读取txt文件的程序.关于getline的用法可以看我之前的博客:2.C++标准库函数:getline函数 定界流输入截取函数 -zobol的 ...
- phpd读取txt文件(自动解析换行)
<form id="form" method="post" action="whois.php"> <?php $newf ...
- php逐行读取txt文件写入数组的方法
使用说明: 采用fopen 方法,逐行读取数据,并使用feof($fp) 判断是否文件截止,最后通过filter() 方法,去除空白行,得到所需数据 $file = fopen("user ...
- 逐行读取txt文件并存入到数组中
get_file_contents_on_line.php $file = fopen("log.txt", "r"); $user=array(); $i=0 ...
- 逐行读取txt文件,分割,写入txt。。。上传,下载
s = [] f = open('querylist.txt','r') #由于我使用的pycharm已经设置完了路径,因此我直接写了文件名 for lines in f: ls = lin ...
- JAVA逐行读取TXT文件
package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...
- 逐行读取txt文件,使用Linq与StreamReader的Readline方法
List<string[]> list = File.ReadLines("YourFile.txt") .Select(r => r.TrimEnd('#')) ...
随机推荐
- NodeJS笔记(二)- 修改模块默认保存路径
参考:nodejs prefix(全局)和cache(缓存)windows下设置 假设nodejs根目录为“D:\nodejs” 如下所示,新建“node_cache”文件夹用来存放全局缓存 该路径下 ...
- C# 如何使用长度来切分字符串
参考网址:https://blog.csdn.net/yenange/article/details/39637211 using System; using System.Collections.G ...
- 1、js比较日期的大小
① html <div class="ptb10"><span>共享开始时间:</span><input type="text& ...
- h5新增标签及css3新增属性
- h5新增的标签 新增元素 说明 video 表示一段视频并提供播放的用户界面 audio 表示音频 canvas 表示位图区域 source 为video和audio提供数据源 track 为vi ...
- 新项目增加gradlew
新项目,增加gradlew git clone http://git.inspur.com/iop/gradle.git git submodule update --init
- 【UML】NO.53.EBook.5.UML.1.013-【UML 大战需求分析】- 组合结构图(Composition Structure Diagram)
1.0.0 Summary Tittle:[UML]NO.52.EBook.1.UML.1.012-[UML 大战需求分析]- 交互概览图(Interaction Overview Diagram) ...
- Java读取resource文件/路径的几种方式
方式一: String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文 ...
- 55.Vue环境搭建
Vue环境搭建 在搭建过程中出现的错误解决办法 https://www.cnblogs.com/lovebing/p/9488198.html cross-env使用笔记 cross- ...
- #WEB安全基础 : HTTP协议 | 0x1 TCP/IP通信
TCP/IP是如何通信的呢? 请看图 用TCP/IP协议族通信时,会通过分层顺序与对方进行通信.发送端从应用层往下走,接受层从链路层往上走. 客户端为了浏览界面在应用层发送请求,为了方便传输在传输层的 ...
- NGUI与特效的层级关系
通过调整特效的 render queue 来解决特效与NGUI界面之间的层级关系问题,用以下脚本解决: using System.Collections.Generic; using UnityEng ...