| | |
layout formatting
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
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.
I'll try to look at it some more this afternoon, unless someone else finds the answer before then.
•
•
Join Date: Dec 2006
Posts: 1,035
Reputation:
Solved Threads: 292
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.
Python Syntax (Toggle Plain Text)
def print_rec(rec): print rec substrs = rec.split() greek_str = " ".join(substrs[1:]) num_chr = 0 for chr in greek_str: print ord(chr), if ord(chr) not in [206, 207]: num_chr += 1 print "\n", num_chr fp = open("./origDict.txt", "r") ## skip heading fp.readline() print_rec(fp.readline()) print_rec(fp.readline()) print_rec(fp.readline())
Last edited by woooee; Oct 14th, 2009 at 8:17 pm.
Linux counter #99383
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
1
#13 Oct 14th, 2009
Please try the following as well. It works on my computer now.
Python Syntax (Toggle Plain Text)
#!/usr/local/bin/python # -*- coding: utf-8 -*- import os, sys #DictManipulation.py written 26/09/09 #See flow_dia_for_new_dict.ods, this directory # # A text file that is an English to Greek dictionary and has 2 columns. # As there are many entries it takes up 25 or so pages. The object is to # produce a file that has 2 x 2 columns and so producing half the number # of pages. The 2nd columb generated should be the next 72 entries following # the first column so the alfa order is maintained. # # A sample of the original file is shown below # #August Αύγουστος (ο) #aunt θεία η #autumn φθινόπωρο (το) #bad, ugly (adj.) άσχημος, -η, -ο #bad, wicked, evil (adj.) κακός, -ή, -ό #bank τράπεζα (η) #basket καλάθι (το) def rpad (orig_string, length): """Adds spaces to the end of a string until it has the desired length""" #Convert to unicode utf-8 because if your default encoding is Ascii the encode step fails ustring = unicode(orig_string,"utf-8","strict") #In a plain string Greek symbols have length = 1 instead of 2 plain_string = ustring.encode("cp737", "replace") spaces_needed = length - len(plain_string) padded_string = orig_string + " " * spaces_needed return padded_string lines_per_page = 73 current_page = 0 line_pointer_a = 1 line_pointer_b = line_pointer_a + lines_per_page write_file = open ("newDict.txt", "w") read_file = open ("origDict.txt", "r") lines_in_file = read_file.readlines () read_file.close() lengh_file = len (lines_in_file) new_line = '\n' while line_pointer_a != (lines_per_page * (current_page + 1 ) + 1): lineA = lines_in_file [line_pointer_a].strip() lineB = lines_in_file [line_pointer_b].strip() #####newString = '%-56s%-60s%s' % ( lineA , lineB, new_line) newString = rpad(lineA,56) + rpad(lineB,60) + new_line write_file.write (newString) print newString print "***Length of the above string is: %d***" % len(newString) line_pointer_a += 1 line_pointer_b += 1 if line_pointer_b >= lengh_file: write_file.close() print 'Job Done' break if line_pointer_a == (lines_per_page * (current_page + 1 ) + 1): current_page += 2 line_pointer_a = (lines_per_page * (current_page ) + 1) line_pointer_b = line_pointer_a + lines_per_page
•
•
Join Date: Sep 2009
Posts: 18
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- RichTextBox to MS Word (with formatting) (C#)
- Word to html (HTML and CSS)
- CSS/XHTML Layout issue when rendering? (HTML and CSS)
- COM Memory Layout, Vtable, and Function Pointer Issues And Questions (C++)
- formatting problems (Windows NT / 2000 / XP)
- New website critique (Website Reviews)
- Redesigned Forum layout (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: New program that sends emails
- Next Thread: A basic question regarding variables
| Thread Tools | Search this Thread |
actionlistener animation array assembly assign c# c++ calculator challenge char character conversion convert count data date datetime dictionary drawing dynamic encryption excel external file filename format fstream ftp gdi+ getline hash html ifstream import input int java javascript jlabel line list listbox memory method mysql namevaluepairs packing parse parsing path perl php program python recursion regex reverse rotation search single singleton slicenotation string text timer unicode unsignedlong user validation xlwt year







