这篇文章主要介绍了关于基于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批量處理dat檔案及科學計算的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

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

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

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


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),