这篇文章将为大家详细讲解有关文本处理用c与用python哪个比较好,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

c++语言:

C++语言实现C++中没有实现split功能的函数,下面用C++STL中的一些函数模拟实现split功能。#include<iostream>#include<string>#include<vector>#include<fstream>usingnamespacestd;/*@in,src:待分割的字符串@in,delim:分隔符字符串@in_out,dest:保存分割后的每个字符串*/voidsplit(conststring&src,conststring&delim,vector<string>&dest){stringstr=src;string::size_typestart=0,index;stringsubstr;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);}}intmain(){ifstreaminfile("test.txt",ios::in);vector<string>results;stringword;stringdelim("");stringtextline;if(infile.good()){while(!infile.fail()){getline(infile,textline);split(textline,delim,results);}}infile.close();vector<string>::iteratoriter=results.begin();while(iter!=results.end()){cout<<*iter++<<endl;}return0;}

python语言:

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

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

关于文本处理用c与用python哪个比较好就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。