You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python读取Visum生成的31x31 .mtx文件遇ValueError求助

Fixing ValueError: not enough values to unpack (expected 5, got 1) with Visum .mtx Files

I’ve run into similar issues with Visum’s non-standard .mtx exports before—here’s how to resolve this:

The error pops up because Visum’s default .mtx format isn’t the standard Matrix Market specification that scipy.io.mmread is designed to handle. Visum uses a simplified custom format for its OD matrices, while mmread requires strict adherence to the official Matrix Market structure (including specific header lines and data formatting).

Two Reliable Solutions:

1. Manually Parse the Visum .mtx File

Since your matrix is small (31×31), it’s easy to read and process the file directly with basic Python:

import numpy as np

# Initialize an empty 31x31 demand matrix (using 0-indexing)
demand_matrix = np.zeros((31, 31), dtype=np.float64)

with open('./saclay/demand_visum.mtx', 'r') as file:
    for line in file:
        line = line.strip()
        # Skip blank lines and any comment lines (Visum often adds these starting with %)
        if not line or line.startswith('%'):
            continue
        # Split the line into origin, destination, and demand value
        # Note: Visum uses 1-indexed zone numbers, so subtract 1 to match Python's 0-indexing
        origin, dest, demand = map(float, line.split())
        demand_matrix[int(origin)-1, int(dest)-1] = demand

# Verify the result
print("Demand Matrix Shape:", demand_matrix.shape)
print(demand_matrix)

This code handles Visum’s typical OD matrix format, where each line contains three values: origin zone ID, destination zone ID, and the travel demand between them.

2. Export a Standard Matrix Market File from Visum

If you’d rather keep using mmread, re-export your matrix from Visum using the official Matrix Market format:

  • In Visum, navigate to the export options for your OD matrix
  • Look for a setting labeled "Matrix Market Format" (exact wording may vary by Visum version)
  • Select this option instead of Visum’s default .mtx export

Once you have the standard Matrix Market file, your original mmread code will work without errors.

Why the Error Happened

scipy.io.mmread expects Matrix Market files to start with a header line like:

%%MatrixMarket matrix coordinate real general

Followed by a line listing the matrix dimensions and number of non-zero entries, then the data lines. Visum’s default .mtx lacks these header lines, so mmread tries to parse the first data line as part of the header—leading to the "not enough values to unpack" error when it can’t find the 5 values it expects from a header line.

内容的提问来源于stack exchange,提问作者Jaâfar BERRADA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:07:54