By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,509 Members | 704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,509 IT Pros & Developers. It's quick & easy.

Python 2.7, Encoding Foreign Language and tkinter

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. # -*- coding: utf8 -*-
  2. from Tkinter import *
  3. import Tkinter as tk
  4. import codecs
  5. from string import *
  6. import sys, win32com.client
  9. #u'\xe1'.encode('utf-8')
  11. root = tk.Tk()
  12. root.title('Tentative 1')
  14. #file = open('Data path to txt file to be read', 'r+')
  16. #sentence =
  17. #sentence = sentence.decode('cp1252', 'strict')
  20. with'Data path to txt file to be read', encoding='latin1') as f:
  21.     sentence =
  24. #if u'\xed' in sentence:
  25.    #print sentence
  27. #else:
  28.     #sentence = sentence.replace("u'\xed'", "-")
  30. def task():
  31.     print '\n', sentence
  33. def replace_all(text, dic):
  34.     for i, j in dic.iteritems():
  35.         text = text.replace(i, j)
  36.     return text
  37. reps = {'^^':'\t', '(':'\t', ')':'\t', 'ISBN:':'\t', '--':'\t', '"':'\t', '.:':'\t', '|':'\t', 'p.':'\t', ',':' '}
  38. txt = replace_all(sentence, reps)
  41. def txt_conversor():
  42.     txt = replace_all(sentence, reps)
  43.     print '\n', txt
  45. log = open('log.txt', 'w')
  47. print(txt_conversor, file1=log)
  49. results = tk.Button(root, text='Results', width=25, command=task)
  50. results.pack()
  51. txt = tk.Button(root, text='Convert Results', width=25, command=txt_conversor)
  52. txt.pack()
  54. 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
Jul 12 '14 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 4,677
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?

Expand|Select|Wrap|Line Numbers
  1. import csv
  2. with open('filename','wb') as fout:
  3.     csvwriter = csv.writer(fout,dialect='excel',quoting=csv.QUOTE_MINIMAL)
  4.     for lines in txt:
  5.         csvwriter.writerow(lines)
Jul 23 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.