[Tutor] how to read data

lina lina.lastname at gmail.com
Sat May 7 15:04:17 CEST 2011


Thanks so much for your detailful information.

I spent sometime in learning before.

On Sat, May 7, 2011 at 8:17 PM, Wayne Werner <waynejwerner at gmail.com> wrote:
> 2011/5/7 lina <lina.lastname at gmail.com>
>>
>> Hi,
>>
>> As you will see I am a new beginner of learning python,
>>
>> I met a problem, hope someone can guide me one or two and help me learn.
>> <snip all your data>
>
> Have you tried anything yet? Do you know how to read in from a file? Can you

I only know the basic about how to read a file and print it out.

## python recolor.py filename

from sys import argv

script, filename = argv

txt = open(filename)

print txt.read()

> go over it line by line? There are several tutorials available, such as Alan
> Gauld's wonderful tutorial: http://www.alan-g.me.uk/ or the Python
> Beginner's Guide has a list of tutorials for both non-programmers and those
> with previous experience: http://wiki.python.org/moin/BeginnersGuide
> Take a look at those and pay special attention to:
> 1. File I/0
> 2. Dictionaries
> 3. The string.replace
> method http://docs.python.org/library/string.html#string.replace
> 4. String slicing
> If I understand what you're asking, you want the large cluster of text at
> the end to, instead of having lots of 'n's, have the number after the hash,
> correct? i.e. n is #000000, which is 0 in base 10, and the first non-zero

those  #000000 are color, I want to recolor them, (so I thought it
might work in matlab (if I provided the matrix).

> value on that line is #212121 (or 2171169 decimal) so you want something
> like this:
> 0 0 0 0 0 0 0 0 0 0 ... 0 0 2171169 ...
> correct?
> If so, for something like this I would probably make two files, one
> containing the first part ('A c #FFFFFF ...') and another containing the
> matrix at the end. The steps I would follow are:
> 1. Open the "translation" file
> 2. For each line in the file
>     a. Get the character on the line
>     b. Get the numeric value the character refers to
>     c. Store these values in a way that allows me to use the character to
> retrieve the number
> 3. close the translation file (not strictly necessary, since Python does
> this for you when the script finishes, but good practice)
> 4. open the matrix file and an output file
> 5. for each line in the matrix
>     a. for each character in my translation
>         i. replace the characters in the line with the numeric value
> followed by a space ( so instead of 0000 you have 0 0 0 0 )
>     b. write the line to the output file
> 6. close the matrix file and the output file (if you don't close the output
> file, there may be no data in it!)
> 7. Go to matlab and do whatever you wanted to.
> Since you sound like you're familiar with matlab, just remember that in
> Python, arrays start with 0, not 1.
>
> If you get stuck at any of these points, come back and let us know
> 1. What you tried
> 2. What you thought should happen
> 3. What really happened
> 4. If an error occurred, the full text of any messages you got.
> HTH,
> Wayne

Thanks again for the programming process. I will try.
-- 
Best Regards,

lina


More information about the Tutor mailing list