Fundamentals 7 min read

How to Read .nc NetCDF Files in Python: Two Simple Methods

This article explains what .nc (netCDF) files are, why they are useful for storing scientific time‑series data, and provides two clear Python solutions—standard reading and direct extraction—complete with installation tips, code examples, and practical guidance.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Read .nc NetCDF Files in Python: Two Simple Methods

Introduction

A fan asked how to read a .nc file in a Python diamond group, and the author shares two solutions.

.nc files differ from regular files and require the third‑party library netCDF4 . Install it with: pip install netCDF4 If the download is slow, use a mirror source:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple netCDF4

1. Basics of .nc Files

.nc stands for netCDF (Network Common Data Form) and is used to store arrays, often for long‑term scientific observations such as meteorology, hydrology, or temperature data.

Using a single .nc file can replace many CSV or TXT files when collecting data at high frequency, making storage and analysis much more convenient.

2. Solutions

Method 1: Conventional Reading

Code example (replace the file path with your own):

# -*- coding: utf-8 -*-
import netCDF4
from netCDF4 import Dataset
nc_obj = Dataset('D:\\tem_e0025_2.nc')
# Inspect variables
print(nc_obj.variables.keys())
lat = nc_obj.variables['lat'][:]
lon = nc_obj.variables['lon'][:]
lev = nc_obj.variables['lev'][:]
time = nc_obj.variables['time'][:]
tem = nc_obj.variables['tem'][:]
# Write to text file
file = open('ppm_lat.txt', 'a')
file.write('lat,lon,time,tem
')
for i in range(len(lat)):
    file.write(str(lat[i])+',')
    file.write(str(lon[i])+',')
    file.write(str(lev[i])+',')
    file.write(str(time[i])+',')
    file.write(str(tem[i])+',
')
file.close()

Simply change the file path to your .nc file.

Method 2: Direct Extraction

Another concise way:

# -*- coding: utf-8 -*-
import netCDF4
from netCDF4 import Dataset
A_temp = Dataset(r'E:\PythonCrawler\有趣的代码\数据分析\最小二乘法数据分析代码和数据\Temperature_20211031.nc')
lat = A_temp['latitude'][:].data
lon = A_temp['longitude'][:].data
temp = A_temp['T'][:].data
depth = A_temp['depth'][:].data
print(len(lat))
print(len(lon))

3. Summary

The author answered the fan’s question about reading .nc files by providing two workable Python methods, enabling readers to extract scientific data efficiently and encouraging further exploration such as regression or clustering.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData Extractionnc filenetCDFscientific data
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.