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

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Reading and putting excel file in a cerain format


    I am attempting to read excel file char by char and putting them in a certain pattern in note pad or word . Here is the description of the problem;
    Extract each character in a fixed length of arrays. The arrays are divided in the left and right context(line). Then start reading/putting a character in the middle until the end of the fixed array . Then push one step backward and continues until it finishes.
    For example ' read<1> s<x>
    let's say the size of the array is 8.
    need to be extracted
    -,-,-,r,e,a,d,0
    -,-,r,e,a,d,s,0
    -,r,e,a,d,s,-,0
    r,e,a,d,s,-,-,1
    e,a,d,s,-,-,-,x
    ...for char in f
    if char.beginswith("<") & endswith(">")
    print rep(char).rjust(8),delimiter=','
    but it should print the value of < > by checking end of the middle letter-that is the vertical value r begins.
    How can I trace this any one try to help me !
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Do you reference an xml file?
    You've tried to give examples of input and output and to be quite clear.

    I cannot understand your post. All I can see is that you ought to use

    where you have

    & endswith(">")

    you'll be better off with
    and char.endswith('>'):

    but if the object you call char is a single character then it won't both start and end with a different chracter.

    good luck.

    good night.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Thanks for your reply but to make more clear


    to make my post more clear .let me change my
    ways of representations.

    I am refering excel file not xml

    There is lots of manully prepared datasets like
    read1sS
    one word per line like the above. I want to automatically assign this words in a fixed length of array like windoing method. by placing each character down as a focus letter and creating features for each character in the left and right context. That is each character is used once as a focus character (F) and associated with the five characters to its left (L1→L4) and the five characters to its right (R1→R4). Therefore each character will be created as much as possible.



    - - - - r e a d s 0
    - - - r e a d s - 0
    - - r e a d s - - 0
    - r e a d s - - - 1
    r e a d s - - - - S



    The function should start from F(the place where r starts) and countinus reading/puting each character until it reaches L4 (the place where s ends) . if the 'F' value is begining of number or upper case those value will be placed at the VALUE column (at the end) like "1" and "S" else put zero and iterates until it finshes and goes to the second line.

    here is my sorts of code that I try

    from xlrd import open_workbook
    spell = open_workbook('/home/mess/sample3.xls')
    for name in spell.sheet_names():
    sheetnew =spell.sheet_by_name(name)
    for i in range(0, 8):
    for char in sheetnew.row(i):
    #if char.beginswith(upper) or (numeric)
    #if row_values isupper() or isnumeric()
    if row_values[] =isupper() or isnumeric()
    # if row_values[] = upper or numeric
    print rep(char).rjust(8), delimiter=','
    else
    print char
    I can not able to generate all line as I expected like in the example
    I hope now my post is relatively clear.

    any one please try to help me
    Last edited by mollam; August 13th, 2013 at 08:29 AM. Reason: not refereing an xml file rather excel file
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    I was thinking of .xlsx files. Both excel and xml. I think we've now learned that excel has nothing to do with the python part of the project. You aren't trying to understand the Component Object Model python libraries.

    Didn't we just have a thread about threaded scrolling text in much this style? I can't find it so i suppose I shouldn't ask you to find it.

    Code:
    import sys
    import io
    
    if sys.version[0] == '2':
        s = u'this is a test.  if it had been a real emergency we would have told you'
    else:
        s = 'this is a test.  if it had been a real emergency we would have told you'
    
    inf = io.StringIO(s)
    
    A = list(' '*30)
    
    c = inf.read(1)
    while c:
        A = A[1:]+[c]
        print(''.join(A))
        c = inf.read(1)
    Sorry this took so many days. Three?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    fantastic effort - many thanks


    Hey really you are committed in facing such challenging issues. I thank you again !
    But still I am expecting to read my file in such pattern from notepad
    because excel is some how not comfortable in playing with it.

    your code works fine.
    But I tried to compile with slight modification in the following ways : to read each character line by line--it failed

    so can I give a path where the file is found ?
    is it possible to read a single line character by character at a time?
    how can I add some condition to compare values and assign in specific place --like putting at the end
    import sys
    import io
    import codecs

    if sys.version[0] == '2':
    #s = (open('/home/mess/sample.txt','U')) // this raises error
    s = U'this is a test. if it had been a real emergency we would have told you'
    #f = codecs.open('/home/mesfin/sample.txt', encoding='utf-8')
    // this raises error
    else:
    s = '/home/mess/sample.txt' #'this is also fails
    #if it had been a real emergency we would have told you'
    inf = io.StringIO(s)
    A = list(' '*45)
    c = inf.read(1)
    while c:
    A = A[1:]+[c]
    print(''.join(A))
    c = inf.read(1)
    [/CODE]
    many thanks !
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Code:
    import sys
    import io
    
    A = list(' '*30)
    
    with open('my_file.csv') as inf:
        c = inf.read(1)
        while c:
            if c not in '\n\r':
                A = A[1:]+[c]
                print(''.join(A))
            c = inf.read(1)
    The with statement seems to cause a lot of indentation. Is it worth while? Yes, I'd advocate

    with open('a file') as inf:
    f(inf)

    In this case i don't want to scare mollam with a function.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    splendid !!


    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo