一个MPI程序,出现这样的错误,仔细看看后续的提示行,是否出现如下的错误:gethostbyname failed
出现这种错误的原因是hosts文件中没有列入当前主机的hostname先看看主机的hostname是什么,用uname -a查看,比如我的是Linux scdzj 2.6.32-696.6.3.el6.x86_64 #1 SMP Wed Jul 12 14:17:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux则第二个字段scdzj是hostname打开/etc/hosts文件,在127.0.0.1的后面加上hostname,保存退出。这个问题即可解决。
stackoverflow. com/questions/23112515/mpich2-gethostbyname-failed
作者: PurpoolPoolObservatory
正则转换大小写
大写转小写: echo ‘ABCDS’ | sed ‘s/[A-Z]/\l&/g’
小写转大写: echo ‘abcds’ | sed ‘s/[a-z]/\u&/g’
pandas挑选并改值
df.loc[df.loc[df['a'] == 1,'b'].index, 'b'] = 3
timestamp to datetime
在pandas中,我先用time.mktime得到了一些标准秒钟数,然后用datetime.datetime.fromstamp将这些秒数转化为可读的时间,注意这个时候的时间格式是timestamp,如果要得到datetime格式的,要用timestamp.to_pydatetime函数。
pandas的date_range时间偏置
想用pandas.data_range(start=’1970′,end=’2020′,freq=’Y’)来生成一个按年分布的时间序列,但实际结果是生成了这样的:1970-12-31,1971-12-31… …我需要的是每年开始的日期,经查找,发现可用如下办法:pandas.date_range(start=’1970′,end=’2020′,freq=pandas.offsets.YearBegin(1))即可。
===========================
也可以简单写成pandas.date_range(start=’1970′,end=’2020′,freq=’1YS’)
matplotlib坐标轴隐藏,刻度和轴名换位置
ax.spines[‘right’].set_color(‘none’)
ax.spines[‘top’].set_color(‘none’)
ax.xaxis.set_ticks_position(‘bottom’)
ax.xaxis.set_label_position(‘top’)
ax.yaxis.set_ticks_position(‘left’)
illustrator无法编辑matplotlib保存的矢量图中的文本
stackoverflow.com /questions/5956182/cannot-edit-text-in-chart-exported-by-matplotlib-and-opened-in-illustrator
是由于默认的fonttype=3引起的,illustrator不能处理这种类型,可改为42(TrueType)就可以了:
>>> import matplotlib as mpl
>>> mpl.rcParams['pdf.fonttype'] = 42
二维指针定义与赋值
方式1
int **p,a[2][3];
p=(int**)a;
方式2
a = (char **)malloc(sizeof(char *) * m); //分配指针数组
a[0] = (char *)malloc(sizeof(char) * m * n);//一次性分配所有空间
free(a[0]);
free(a);
NRC里定义向量和矩阵
向量和矩阵一般下表从1开始,不注意的话很容易导致段错误。
int n=3;
float *d=vector(1,n);
float **v=matrix(1,n,1,n);
float **a=convert_matrix(&array[0],1,n,1,n);//array是一个预先存在的一维数组
free_convert_matrix(a,1,n,1,n);
free_matrix(v,1,n,1,n);
free_vector(d,1,n);
ICONV字符GB2312转换UTF8
指令:
#iconv -f GB2312 -t UTF-8 gb1.txt >gb2.txt
将gb1里的编码从GB2312转化成UTF-8 并重定向到gb2.txt
除了iconv命令,我们在linux系统下的man page的第三节还可以看到一组iconv函数。它们分别是
iconv_t iconv_open(const char *tocode, const char *fromcode);
size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
int iconv_close(iconv_t cd);
iconv_open函数用来打开一个编码转换的流,iconv函数的作用是实际进行转换,iconv_close函数的作用就是关闭这个流。实际用法参见下面的例子,下面是一个将UTF-8码转换成GBK码的例子,我们假设已经有了一个uft8编码的输入缓冲区inbuf以及这个缓冲区的长度inlen。
iconv_t cd = iconv_open( "GBK", "UTF-8");
char *outbuf = (char *)malloc(inlen * 4 );
bzero( outbiuf, inlen * 4);
char *in = inbuf;
char *out = outbuf;
size_t outlen = inlen *4;
iconv(cd, &in, (size_t *)&inlen, &out,&outlen);
outlen = strlen(outbuf);
printf("%sn",outbuf);
free(outbuf);
iconv_close(cd);
非常值得注意的地方是:iconv函数会修改参数in和参数out指针所指向的地方,也就是说,在调用iconv函数之前,我们的in和inbuf指针以及out和outbuf指针指向的是同一块内存区域,但是调用之后out指针所指向的地方就不是outbuf了,同理in指针。所以要
char *in = inbuf;
char *out = outbuf;
另存一下,使用或者释放内存的时候也要使用原先的那个指针outbuf和inbuf。
例子:
#iconv -f gb2312 -t utf8 buy_msg.htm >buy_msg.html
将buy_msg.htm 内的字符GB2312转换为UTF8