Python 2.7, Encoding Foreign Language and tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santoznma
    New Member
    • Jul 2014
    • 1

    Python 2.7, Encoding Foreign Language and tkinter

    Hello World!

    I am currently writing a script that reads an input file (in txt) and outputs a tab delimited file, which will be converted to csv ( to input to a SQLite Database). It recognizes special characters in a dictionary and inserts a tab '\t' every time it finds a character like '))' or '|'. I am Portuguese and I'm new to programming and Python, but after all the encoding problems ( because we have odd characters like á, à, ã, ê, and so on) I did manage to print the accents and the tabs within the window. But now I just can't figure out how to output the printed function into a txt file! I tried the from __future__ import print_function and I just can't figure out how to output this. Maybe you guys can help me on this and tell me what I'm doing wrong and correct me! Please don't be bad at me, I am just starting to program and I need the help of experienced programmers like you guys!

    Here is the code:
    Code:
    # -*- coding: utf8 -*-
    from Tkinter import *
    import Tkinter as tk
    import codecs
    from string import *
    import sys, win32com.client
    
    
    #u'\xe1'.encode('utf-8')
    
    root = tk.Tk()
    root.title('Tentative 1')
    
    #file = open('Data path to txt file to be read', 'r+')
    
    #sentence = file.read()
    #sentence = sentence.decode('cp1252', 'strict')
    
    
    with codecs.open('Data path to txt file to be read', encoding='latin1') as f:
        sentence = f.read()
    
    
    #if u'\xed' in sentence:
       #print sentence
    
    #else:
        #sentence = sentence.replace("u'\xed'", "-")
    
    def task():
        print '\n', sentence
       
    def replace_all(text, dic):
        for i, j in dic.iteritems():
            text = text.replace(i, j)
        return text
    reps = {'^^':'\t', '(':'\t', ')':'\t', 'ISBN:':'\t', '--':'\t', '"':'\t', '.:':'\t', '|':'\t', 'p.':'\t', ',':' '}
    txt = replace_all(sentence, reps)
    
    
    def txt_conversor():
    	txt = replace_all(sentence, reps)
    	print '\n', txt
    
    log = open('log.txt', 'w')
    
    print(txt_conversor, file1=log)
       
    results = tk.Button(root, text='Results', width=25, command=task)
    results.pack()
    txt = tk.Button(root, text='Convert Results', width=25, command=txt_conversor)
    txt.pack()
    
    root.mainloop()
    It says that I have a syntax error on "print '\n', sentence" but I know that isn't the main problem.

    Input File:
    Correia, Teresa Pinto; Henriques, Virgínia; Julião, Rui Pedro^^ (2013)), IX Congresso da Geografia Portuguesa – Geografia: Espaço, Natureza, Sociedade e Ciência--, ISBN: 978-972-99436-6-9, |Lisboa: Associação Portuguesa de Geógrafos. p. 977 e-Book


    Dominguez, L.; Aliste, J; Ibáñez Martinez; Natário, M.; Fernandes, Gonçalo Poeta^^ (2013) – "Estudio Socioeconomico de la Frontera entre Portugal y España", |Edita Riet, --Salamanca. ISBN: 978-84-7797-403-1
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Do you need to write to a tab-delimited file? You can write to a file and use \t for tab, but if you're converting it to csv, why not skip the conversion and write directly to a csv?

    [code=python]
    import csv
    with open('filename' ,'wb') as fout:
    csvwriter = csv.writer(fout ,dialect='excel ',quoting=csv.Q UOTE_MINIMAL)
    for lines in txt:
    csvwriter.write row(lines)
    [/code]

    Comment

    Working...