Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:26+00:00 2026-05-26T22:57:26+00:00

I would like to read a 2d array of integers from stdin (or from

  • 0

I would like to read a 2d array of integers from stdin (or from a file) in Python.

Non-working code:

from StringIO import StringIO
from array import array

# fake stdin
stdin = StringIO("""1 2
3 4
5 6""")

a = array('i')
a.fromstring(stdin.read())

This gives me an error: a.fromstring(stdin.read())
ValueError: string length not a multiple of item size

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:57:26+00:00Added an answer on May 26, 2026 at 10:57 pm

    Several approaches to accomplish this are available. Below are a few of the possibilities.

    Using an array

    From a list

    Replace the last line of code in the question with the following.

    a.fromlist([int(val) for val in stdin.read().split()])
    

    Now:

    >>> a
    array('i', [1, 2, 3, 4, 5, 6])
    

    Con: does not preserve 2d structure (see comments).

    From a generator

    Note: this option is incorporated from comments by eryksun.

    A more efficient way to do this is to use a generator instead of the list. Replace the last two lines of the code in the question with:

    a = array('i', (int(val) for row in stdin for val in row.split()))
    

    This produces the same result as the option above, but avoids creating the intermediate list.

    Using a NumPy array

    If you want the preserve the 2d structure, you could use a NumPy array. Here’s the whole example:

    from StringIO import StringIO
    import numpy as np
    
    # fake stdin
    stdin = StringIO("""1 2
    3 4
    5 6""")
    
    a = np.loadtxt(stdin, dtype=np.int)
    

    Now:

    >>> a
    array([[1, 2],
           [3, 4],
           [5, 6]])
    

    Using standard lists

    It is not clear from the question if a Python list is acceptable. If it is, one way to accomplish the goal is replace the last two lines of the code in the question with the following.

    a = [map(int, row.split()) for row in stdin]
    

    After running this, we have:

    >>> a
    [[1, 2], [3, 4], [5, 6]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array in memory, read from a file. I would like
I would like to read a text file into an array of strings using
I would like to read coordinates from an from an input file. An example
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
Using C I would like to read in the contents of a text file
I would like to create a new System.Windows.Xps.Packaging.XpsDocument object from byte array, as I
In Python, long integers have unlimited precision. I would like to write a 16
I would like to read data from a plist, add some elements and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.