栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

文本处理用c还是用python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文本处理用c还是用python

文本处理python与c的对比:如下

c++语言:

C++语言实现C++中没有实现split功能的函数,下面用C++ STL中的一些函数模拟实现split功能。#include #include #include #include using namespace std;void split(const string& src, const string& delim, vector& dest){
string str = src;
string::size_type start = 0, index;
string substr;
index = str.find_first_of(delim, start);//在str中查找(起始:start) delim的任意字符的第一次出现的位置
while(index != string::npos)
{
substr = str.substr(start, index-start);
dest.push_back(substr);
start = str.find_first_not_of(delim, index);//在str中查找(起始:index) 第一个不属于delim的字符出现的位置
if(start == string::npos) return;
index = str.find_first_of(delim, start);
}}int main(){
ifstream infile("test.txt", ios::in);
vector results;
string word;
string delim(" ");
string textline;
if(infile.good())
{
while(!infile.fail())
{
getline(infile, textline);
split(textline, delim, results);
}
}
infile.close();
vector::iterator iter = results.begin();
while(iter != results.end())
{
cout<<*iter++<

python语言:

在Python中有专门的函数split()对字符串进行分割,实现较为简单myfile = open('test.txt', 'r')allWords = []line = myfile.readline()while line:
list = line.split(' ')
for word in list:
if word[-1]=='
':
allWords.append(word[:-1])#去掉行末的'
'
else:
allWords.append(word)
line = myfile.readline()myfile.close()print allWords

相比较而言,(抛开运行效率不说),开发效率比较好的是Python,然后是c++,(但是一旦c++这些方法提前包装好了,也是很不错的)。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/806406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号