[Baypiggies] Is this a conversion problem? Can I get help to resolve this please?

Joshua Gallagher joshua.gallagher at gmail.com
Wed Aug 27 01:14:07 CEST 2008


Hi David,

When you read and write into a file, you read and write at offsets.
You can think of the offset as a moving cursor in the file.  In other
words, if you create a new file and write 10 bytes in, your cursor is
10-bytes into the file.  If you perform another write, the write
starts at byte 10.  By extension, if your cursor is at byte 10 of this
new file and you do a read, there's nothing at the end of the file to
read.

If you've written to a file and want to reread it without closing it
you need to seek (http://docs.python.org/lib/bltin-file-objects.html)
if you have a file object or lseek
(http://docs.python.org/lib/os-fd-ops.html) if you have a file
descriptor.

Joshua



On Tue, Aug 26, 2008 at 3:56 PM, David Elsen <elsen.david08 at gmail.com> wrote:
> Alex,
>
> Thank you for your reply.
>
> Actually I had modified the text file before posting. It has four words in
> one line seperated by space.  I was using the split with  space to get all
> four words from a line. I was appending one word to my list (list of
> strings).
>
> I was wondering once I am using split function whether it converts the
> "numbers" from my text file to some particular formats. I was trying to cast
> the split output with "map", "long" etc, but it gives the error message
> that for it can not accept the non-string input.
>
> I tried to look around for split reference, but nowhere got its return type
> format information. It seems like string ['0xdeadfeed']. But it does not
> seem to be. If it is really string type, why cast from "map", "int", or
> "long" is giving me error message.
>
> I apologize for becoming the time sink. I am ready to become the time
> source.
>
> I have one more question though related to my same script. Please forgive me
> about this. If I am trying to write my text file in the same script and then
> tring to read back. For some reason, read file handle does not have any
> data.
>
> But if I have written to the text file and saved it and then trying to open
> it in read mode, I am able to read the data. Is there any delay required
> after writing the data to a file?
>
> Thanks,
> David
>
> Thanks again,
> David
>
>
>
>
>
>
> On Tue, Aug 26, 2008 at 1:26 PM, Alex Martelli <aleax at google.com> wrote:
>>
>> You should really study some of the basics of Python -- e.g. via the
>> mailing list http://mail.python.org/mailman/listinfo/tutor or any of
>> the other resources listed at
>> http://wiki.python.org/moin/BeginnersGuide -- this mailing list is for
>> the Bay Area Python Interest Group and not particularly suitable for
>> your questions.  In this case, you're appending to your list reg items
>> that are lists of strings (with one item only in each sublist,
>> apparently) and you apparently want to convert that list of lists of
>> strings to a list of integers -- and give no indication whatsoever
>> about what you mean to happen when one of the sublists has more than
>> one item (i.e. any of the text lines in file ask_it.txt [apart from
>> the first four] has more than one word), or any of the words are not a
>> correct representation for an integer, and so forth.  If you know the
>> "more than one word in a line" can never happen, then it makes no
>> sense to have that "datas = line.split()" call -- why are you
>> splitting unless >1 word is possible?!
>>
>> http://www.catb.org/~esr/faqs/smart-questions.html is a document
>> really worth studying (quite apart from Python, ANY time you need to
>> ask technical questions) and it includes worthy quotes such as "What
>> we are, unapologetically, is hostile to people who seem to be
>> unwilling to think or to do their own homework before asking
>> questions. People like that are time sinks — they take without giving
>> back, and they waste time we could have spent on another question more
>> interesting and another person more worthy of an answer."
>>
>>
>> Alex
>>
>> On Tue, Aug 26, 2008 at 1:02 PM, David Elsen <elsen.david08 at gmail.com>
>> wrote:
>> > Hi all,
>> >
>> > I have a test script as follows:
>> >
>> > import os
>> > import string
>> > cmd = './testtool  dump'
>> > res = os.popen(cmd).read()
>> > #print res
>> > #fout = open("ask_it.txt", "w")
>> > #fout.write(res)
>> > #fout.close
>> > reg_reset_val = []
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> > reg_reset_val.append(0xdeadfeed)
>> >
>> > print 'reg default values'
>> > for index, item in enumerate(reg_reset_val):
>> >  print index, item
>> >
>> > fin = open ("ask_it.txt", "r")
>> > i = 0
>> > reg = []
>> > linelist = fin.readlines()
>> > lines = len(linelist)
>> > for line in linelist:
>> >  i = i +1
>> >  if (i >= 5): # First four lines are bogus
>> >   datas = line.split()
>> >   reg.append(datas)
>> >   if i == lines:
>> >    break
>> > print 'the reg list is'
>> > for j, item in enumerate(reg):
>> >  print j, item
>> > print 'reg[0]'
>> > print reg[0]
>> > print 'reg_reset_val[0]0x%x'%reg_reset_val[0]
>> > #print reg_reset_val[0]
>> > if (reg[0] == reg_reset_val[0]):
>> >  print 'test passed'
>> > else:
>> >  print 'test failed'
>> >
>> >
>> > Calling to driver via IOCTL Control Memory Dump buf addr = 0x804d008.
>> > Returned from calling driver via IOCTL.
>> > IOCTL CTL Mem Dump char count
>> > Control Memory Dump:  Address 0xf8812000
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > 0xdeadfeed
>> > I am expecting the "reg[0]" and "reg_reset_val[0]" to be the same. But
>> > reg[0] is list and has ['0xdeadfeed'] in it and reg_reset_val[0] has
>> > 0xdeadfeed in it. How can I convert the reg[0] to make it normal
>> > 0xdeadfeed.
>> >
>> > Thanks,
>> > David
>> >
>> >
>> >
>> > _______________________________________________
>> > Baypiggies mailing list
>> > Baypiggies at python.org
>> > To change your subscription options or unsubscribe:
>> > http://mail.python.org/mailman/listinfo/baypiggies
>> >
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>


More information about the Baypiggies mailing list