#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0

    Cool DataManipulation


    Hi,
    I have a .txt file containing some data and i want python to read from it and produce matrices out of this in order to create some graphs after. I have already succeded to read and reproduce the file using the 'string.split' command but i dont have the best result because it produces 1x1 matrices. Can somebody please give me some hint ?

    Thank in advance anybody who will contribute to this,

    Nikos
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    If you give us a hint as to the format of the text file we can give you a hint too
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0

    More hint...


    Originally Posted by Grim Archon
    If you give us a hint as to the format of the text file we can give you a hint too
    My .txt file contains alligned data in columns produced by 'fprintf' command and i want python to take these columns and to reproduce them as lists(tables) in order to make some graphs with matplotlib.
    Thanks anyway for your interest,

    Nikos
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Without knowing what the format is I guess you would do something like:
    Code:
    datalines = open('datafile','r').readlines()
    datarows = []
    for n in datalines:
        datarows.append(n.split(',')) #assumes separator is comma
    each element of datarows contains the column data
    cell = datarows[row][column]

    grim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0
    Originally Posted by Grim Archon
    Without knowing what the format is I guess you would do something like:
    Code:
    datalines = open('datafile','r').readlines()
    datarows = []
    for n in datalines:
        datarows.append(n.split(',')) #assumes separator is comma
    each element of datarows contains the column data
    cell = datarows[row][column]

    grim

    Your code is simpler than mine but still has the same result. I cannot take [1][2] element for example. The columns number goes out of range after [0]!! I want the result to be 8x8 matrix and not 8x1 matrix!! Is there any way for it to be done ?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    The columns number goes out of range after [0]!! I want the result to be 8x8 matrix and not 8x1 matrix!!
    Does the text file describe an 8x8 matrix (e.g.

    Code:
    1, 2, 3, 4, 5, 6, 7, 8
    1, 1, 1, 1, 1, 1, 1, 1
    2, 2, 2, 2, 2, 2, 2, 2
    ...
    ) or does it finish somewhere else but you need an 8x8 matrix which fills in the blanks (e.g.

    Code:
    1, 2, 3, 4
    1, 2, 1
    1, 2, 3, 4, 5, 6, 7, 8
    2, 3, 4
    ) ?
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Yep,
    Without you sharing the text file with us we can't guess its format so can't help !!!

    Post your text file (or a representative sample if its big!).

    grim
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0
    ok guys i will post the .txt file i want to manipulate so it will disolve the misundestandings....
    So, my goal is python to reproduce the file ( originally produced by 'fprintf' command ) in lists so i can take ,for example , the first and the third column and plot them using matplotlib.

    I really appreciate everybody`s interest towards my problem,

    Nikos
    Attached Files
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0
    In order to see the .txt file i suggest not to use the Notepad editor cause the result is not the ideal.
    I use the Textpad editor or m-editor from Matlab

    Nikos
  18. #10
  19. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    This should work - adjust the pattern as needed..
    Code:
    import re
    number = "(\d*\.*\d*)" #defines a number
    pattern = re.compile('%(NUM)s\s+%(NUM)s\s+%(NUM)s\s+%(NUM)s\s+%(NUM)s\s+%(NUM)s\s+%(NUM)s'%{'NUM':number}) #a valid line containing 7 numbers
    datalines = open('testmulti-3.txt','r').readlines()
    datarows = []
    
    for n in datalines:
        line = n.strip()
        if len(line):
            if 'degree' in line : continue
            numbers = pattern.search(line)
            if numbers:
                datarows.append(numbers.groups())
    print datarows
    grim
  20. #11
  21. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Slightly better definition for number:
    number = "(\d*\.{0,1}\d*)"

    grim
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0

    Thumbs up


    Thanks Grim , you defenately know how to program in Python!!

    Best Regards,

    Nikos
  24. #13
  25. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Happy to help
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    11
    Rep Power
    0

    convert list


    Is it possible from the above code to get floats and not strings ?
    without adding lots of lines ??
    Attached Files
    • File Type: txt np.txt (493 Bytes, 287 views)
  28. #15
  29. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    The simplest way might be to do this:
    Code:
    datarows.append(map(float,numbers.groups()))
    grim

IMN logo majestic logo threadwatch logo seochat tools logo