layout formatting

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
0
  #11
Oct 14th, 2009
Now that we can recreate the problem I'm confident that we're close to finding a way around it. I just need to learn a little more about how to format a unicode string. Maybe something to do with encoding or decoding it to set the length we want when formatting.

I'll try to look at it some more this afternoon, unless someone else finds the answer before then.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 292
woooee woooee is offline Offline
Veteran Poster
 
1
  #12
Oct 14th, 2009
You would have to step through the unicode and count the number of actual letters, and then add enough spaces to the string to make it the length you want. This appears to work, but you'll have to test for yourself.
  1. def print_rec(rec):
  2. print rec
  3. substrs = rec.split()
  4. greek_str = " ".join(substrs[1:])
  5. num_chr = 0
  6. for chr in greek_str:
  7. print ord(chr),
  8. if ord(chr) not in [206, 207]:
  9. num_chr += 1
  10.  
  11. print "\n", num_chr
  12.  
  13.  
  14. fp = open("./origDict.txt", "r")
  15. ## skip heading
  16. fp.readline()
  17.  
  18. print_rec(fp.readline())
  19. print_rec(fp.readline())
  20. print_rec(fp.readline())
Last edited by woooee; Oct 14th, 2009 at 8:17 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
1
  #13
Oct 14th, 2009
Please try the following as well. It works on my computer now.
  1. #!/usr/local/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os, sys
  4. #DictManipulation.py written 26/09/09
  5. #See flow_dia_for_new_dict.ods, this directory
  6. #
  7. # A text file that is an English to Greek dictionary and has 2 columns.
  8. # As there are many entries it takes up 25 or so pages. The object is to
  9. # produce a file that has 2 x 2 columns and so producing half the number
  10. # of pages. The 2nd columb generated should be the next 72 entries following
  11. # the first column so the alfa order is maintained.
  12. #
  13. # A sample of the original file is shown below
  14. #
  15. #August Αύγουστος (ο)
  16. #aunt θεία η
  17. #autumn φθινόπωρο (το)
  18. #bad, ugly (adj.) άσχημος, -η, -ο
  19. #bad, wicked, evil (adj.) κακός, -ή, -ό
  20. #bank τράπεζα (η)
  21. #basket καλάθι (το)
  22.  
  23. def rpad (orig_string, length):
  24. """Adds spaces to the end of a string until it has the desired length"""
  25. #Convert to unicode utf-8 because if your default encoding is Ascii the encode step fails
  26. ustring = unicode(orig_string,"utf-8","strict")
  27. #In a plain string Greek symbols have length = 1 instead of 2
  28. plain_string = ustring.encode("cp737", "replace")
  29. spaces_needed = length - len(plain_string)
  30. padded_string = orig_string + " " * spaces_needed
  31. return padded_string
  32.  
  33. lines_per_page = 73
  34. current_page = 0
  35. line_pointer_a = 1
  36. line_pointer_b = line_pointer_a + lines_per_page
  37.  
  38. write_file = open ("newDict.txt", "w")
  39. read_file = open ("origDict.txt", "r")
  40. lines_in_file = read_file.readlines ()
  41. read_file.close()
  42. lengh_file = len (lines_in_file)
  43. new_line = '\n'
  44.  
  45.  
  46. while line_pointer_a != (lines_per_page * (current_page + 1 ) + 1):
  47. lineA = lines_in_file [line_pointer_a].strip()
  48. lineB = lines_in_file [line_pointer_b].strip()
  49. #####newString = '%-56s%-60s%s' % ( lineA , lineB, new_line)
  50. newString = rpad(lineA,56) + rpad(lineB,60) + new_line
  51. write_file.write (newString)
  52. print newString
  53. print "***Length of the above string is: %d***" % len(newString)
  54. line_pointer_a += 1
  55. line_pointer_b += 1
  56. if line_pointer_b >= lengh_file:
  57. write_file.close()
  58. print 'Job Done'
  59. break
  60. if line_pointer_a == (lines_per_page * (current_page + 1 ) + 1):
  61. current_page += 2
  62. line_pointer_a = (lines_per_page * (current_page ) + 1)
  63. line_pointer_b = line_pointer_a + lines_per_page
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

layout formatting

 
0
  #14
Oct 16th, 2009
Hi Guys
Thankyou both for your efforts.
I have impilmented your mod. and it works great.
I have been trying to use the u fuction to try and influence the interpreter that a unicode string requires formatting with no success.
There must be a way of acheiving the desired effect using the format operators!!!??

Thanks again for your help
Reply With Quote Quick reply to this message  
Reply

Tags
string, unicode

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC