


Batch processing of dat files and scientific calculation methods based on 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,简直神器一般的存在,网上搭建的资料也太多。
相关推荐:
The above is the detailed content of Batch processing of dat files and scientific calculation methods based on python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
