python将字符串list写入excel和txt的实例

yipeiwu_com6年前Python基础

docs = [‘icassp improved human face identification using frequency domain representation facial asymmetry', ‘pattern recognition unsupervised methods classification hyperspectral images low spatial resolution', ‘iscas post layout watermarking method ip protection', ‘computers mathematics applications tauberian theorems product method borel cesàro summability', ‘ieee t. geoscience remote sensing mirs all-weather 1dvar satellite data assimilation retrieval system']

将docs写入excel

docs = [doc.encode('latin-1', 'ignore') for doc in docs]
# convert list to array
docs_array = np.array(docs)
print(type(docs_array))
# saving...
np.savetxt('/Users/Desktop/portrait/jour_paper_docs.csv', docs_array, fmt='%s', delimiter=',')
print('Finish saving csv file')

结果

将docs写入txt

def save(filename, docs):
 fh = open(filename, 'w', encoding='utf-8')
 for doc in docs:
  fh.write(doc)
  fh.write('\n')
 fh.close()
save('/Users/Desktop/portrait/jour_paper_docs.txt', docs)

结果

以上这篇python将字符串list写入excel和txt的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python一行sql太长折成多行并且有多个参数的方法

sql语句 有一个非常长的sql,用编辑器打开编写的时候太长了导致编写非常吃力,而且容易错乱,我想做的是把A,B,C三个变量赋值到sql中的字段中去 A=1 B=2 C=3 sql...

Python代码实现KNN算法

kNN算法是k-近邻算法的简称,主要用来进行分类实践,主要思路如下: 1.存在一个训练数据集,每个数据都有对应的标签,也就是说,我们知道样本集中每一数据和他对应的类别。 2.当输入一个...

python实现文本进度条 程序进度条 加载进度条 单行刷新功能

python实现文本进度条 程序进度条 加载进度条 单行刷新功能,具体内容如下所示: 利用time库来替代某个程序 的进行过程,做实例, 思路是,简单打印出来程序进度 单行刷新关键是\r...

利用python实现简易版的贪吃蛇游戏(面向python小白)

利用python实现简易版的贪吃蛇游戏(面向python小白)

引言 作为python 小白,总是觉得自己要做好百分之二百的准备,才能开始写程序。以至于常常整天在那看各种语法教程,学了几个月还是只会print('hello world')。 这样...

Python实现的矩阵类实例

本文实例讲述了Python实现的矩阵类。分享给大家供大家参考,具体如下: 科学计算离不开矩阵的运算。当然,python已经有非常好的现成的库:numpy(numpy的简单安装与使用可参考...