ValueError: string length not a multiple of item size ?

This is the place for queries that don't fit in any of the other categories.

ValueError: string length not a multiple of item size ?

Postby verstapp » Mon Jul 22, 2013 12:26 pm

wishing to write/read a list of integers to a binary file, i started with Alex's/Nadia's answers from
http://stackoverflow.com/questions/8078 ... -in-python
the example worked perfectly, as did the initial write to generate my file.
however, when i tried to read my 200MB file i got
ValueError: string length not a multiple of item size
python 2.7, win7.
any ideas?
Last edited by Yoriz on Mon Jul 22, 2013 4:34 pm, edited 1 time in total.
Reason: changed title
verstapp
 
Posts: 9
Joined: Mon Jul 15, 2013 3:08 am

Re: read/write list of ints

Postby Crimson King » Mon Jul 22, 2013 4:30 pm

verstapp wrote:wishing to write/read a list of integers to a binary file, i started with Alex's/Nadia's answers from
http://stackoverflow.com/questions/8078 ... -in-python
the example worked perfectly, as did the initial write to generate my file.
however, when i tried to read my 200MB file i got
ValueError: string length not a multiple of item size
python 2.7, win7.
any ideas?


Hello verstapp, could you post your code and a couple of lines of the file you're reading?
User avatar
Crimson King
 
Posts: 87
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: ValueError: string length not a multiple of item size ?

Postby verstapp » Mon Jul 22, 2013 8:11 pm

the code, the error message points to the fromstring line:
Code: Select all
def readinprimes4(): #read binary file of primes.
    p=[]
    inp=open('primes','r')
    aa=array('i')
    aa.fromstring(inp.read())
    raw_input('finished reading in')
    inp.close()
    p=[int(i) for i in aa]
    return p


its a binary file, so no official 'lines', but the start of the file in hex editor Neo looks like:
02 00 00 00 03 00 00 00 05 00 00 00 07 00 00 00
0b 00 00 00 0d 00 00 00 11 00 00 00 13 00 00 00
17 00 00 00 1d 00 00 00 1f 00 00 00 25 00 00 00

the end of the file looks like:

77 c8 9a 3b 7d c8 9a 3b b3 c8 9a 3b bd c8 9a 3b
f5 c8 9a 3b fb c8 9a 3b 07 c9 9a 3b 0d c9 9a 3b
11 c9 9a 3b 35 c9 9a 3b 8b c9 9a 3b 95 c9 9a 3b
b9 c9 9a 3b c1 c9 9a 3b 07 ca 9a
verstapp
 
Posts: 9
Joined: Mon Jul 15, 2013 3:08 am

Re: ValueError: string length not a multiple of item size ?

Postby ochichinyezaboombwa » Tue Jul 23, 2013 12:47 am

One thing is certain : you don't open it as a binary. Should be
Code: Select all
inp=open('primes','rb')

I think basically this should do it; however if the problem persists please show how you create your data.

PS: read this and this. Or (much better) forget about "binary data".
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: ValueError: string length not a multiple of item size ?

Postby verstapp » Tue Jul 23, 2013 10:31 am

thanks, ochi.
that fixed it. i can only assume that the stackoverflow responders assumed a reader would know the correct incantation for their particular os and would edit accordingly. their example code said 'wb' to write but only 'r' to read.
i think i'll stick with binary for this particular example due to the 5-6 times speed increase over f.readlines().
thanks again.
verstapp
 
Posts: 9
Joined: Mon Jul 15, 2013 3:08 am


Return to General Coding Help

Who is online

Users browsing this forum: cairo, Marbelous, Williamtt97 and 3 guests