搜索
首页后端开发Python教程基于python批量处理dat文件及科学计算的方法

基于python批量处理dat文件及科学计算的方法

May 08, 2018 pm 02:15 PM
python处理文件

这篇文章主要介绍了关于基于python批量处理dat文件及科学计算的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

摘要:主要介绍一些python的文件读取功能,文件内容修改,文件名后缀更改等操作。

批处理文件功能

import os
path1 = 'C:\\Users\\awake_ljw\\Documents\\python for data analysis\\test1'
path2 = 'C:\\Users\\awake_ljw\\Documents\\python for data analysis\\test2'
filelist = os.listdir(path1)
for files in filelist:
 Olddir = os.path.join(path1,files)
 filename = os.path.splitext(files)[0]
 filetype = os.path.splitext(files)[1]
 print Olddir
 file_test = open(Olddir,'r')
 Newdir = os.path.join(path2,str(filename)+'.csv')
 print Newdir
 file_test2 = open(Newdir,'w')
 for lines in file_test.readlines():
 strdata = ",".join(lines.split('\t'))
 file_test2.write(strdata)
 file_test.close()
 file_test2.close()

os模块是python最基础的模块之一,一般用于文件处理等操作。上面这段代码主要就是将dat文件转化为csv文件,同时保证csv可读。一般txt文件不能通过直接改后缀改变呈csv文件格式,一般会造成文件不可读。csv文件一般通过逗号分隔文本,数据处理起来较得心应手,可以直接改后缀得到xlsx文件,一般excel也可读。

科学计算

matlab作为一门科学计算编程语言,在科学计算的应用实在广泛,包括webread等强大的函数用起来十分顺手,但matlab是商业软件,并不免费。其实,python在科学计算效率或函数库功能包括其绘图功能、图像处理都很强大,(相比matlab,python的调色板更出色)。以下列举一些数据文件读取,绘图的一些基本操作作为参考。

数据提取及绘图

#数据提取
import os 
import pandas as pd
import numpy as np
number = -1;
sudu=np.zeros(5247*5,dtype=float).reshape(5247,5)
for files in filelist1:
 number +=1
 data = pd.read_csv(str(number+1)+'a.csv')
 sudu[:,number]=data['velocity']
 x = data['x']
 y = data['y']
a = sudu[0:5184,0].reshape(81,64)
%matplotlib inline
import matplotlib.pyplot as plt
extent = [np.min(x),np.max(x),np.min(y),np.max(y)]
plt.subplot(231)
u0 = sudu[0:5184,0].reshape(81,64)
plt.imshow(u0,extent=extent,origin='lower')
plt.subplot(232)
u1 = sudu[0:5184,1].reshape(81,64)
plt.imshow(u1,extent=extent,origin='lower')
plt.subplot(233)
u2 = sudu[0:5184,2].reshape(81,64)
plt.imshow(u2,extent=extent,origin='lower')
plt.subplot(234)
u3 = sudu[0:5184,3].reshape(81,64)
plt.imshow(u3,extent=extent,origin='lower')
#plt.axis("equal")
plt.subplot(235)
u4 = sudu[0:5184,4].reshape(81,64)
plt.imshow(u4,extent=extent,origin='lower')
plt.subplot(236)
u5 = sudu[0:5184,4].reshape(81,64)
plt.imshow(u5,extent=extent,origin='lower')
#contour
cs = plt.contour(u5, 20,extent = extent)
plt.xlim(-0.8,0.8)
plt.ylim(0.6,2.2)
plt.axis('equal')

python的科学计算功能与matlab及其相似,python有几点不同在于

1.python有元组的数据类型,元组不同于列表,元组不可更改

2.python的数据检索使用[]

总而言之,python的数据形式及其丰富。

numpy以及pandas是python用于数据处理的两个库,具体使用方法主要推荐python科学计算这本书。matplotlib用于绘图,刚也说了,其调色板很厉害哦,图像质量不错。

预告:代码运行环境均为jupyter notebook,简直神器一般的存在,网上搭建的资料也太多。

相关推荐:

Python实现的求解最小公倍数算法示例

以上是基于python批量处理dat文件及科学计算的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表的内存足迹与python数组的内存足迹相比如何?列表的内存足迹与python数组的内存足迹相比如何?May 02, 2025 am 12:08 AM

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

部署可执行的Python脚本时,如何处理特定环境的配置?部署可执行的Python脚本时,如何处理特定环境的配置?May 02, 2025 am 12:07 AM

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

您如何切成python阵列?您如何切成python阵列?May 01, 2025 am 12:18 AM

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

在什么情况下,列表的表现比数组表现更好?在什么情况下,列表的表现比数组表现更好?May 01, 2025 am 12:06 AM

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

如何将Python数组转换为Python列表?如何将Python数组转换为Python列表?May 01, 2025 am 12:05 AM

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具