473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Python 2.7, Encoding Foreign Language and tkinter

1 New Member
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
  7.  
  8.  
  9. #u'\xe1'.encode('utf-8')
  10.  
  11. root = tk.Tk()
  12. root.title('Tentative 1')
  13.  
  14. #file = open('Data path to txt file to be read', 'r+')
  15.  
  16. #sentence = file.read()
  17. #sentence = sentence.decode('cp1252', 'strict')
  18.  
  19.  
  20. with codecs.open('Data path to txt file to be read', encoding='latin1') as f:
  21.     sentence = f.read()
  22.  
  23.  
  24. #if u'\xed' in sentence:
  25.    #print sentence
  26.  
  27. #else:
  28.     #sentence = sentence.replace("u'\xed'", "-")
  29.  
  30. def task():
  31.     print '\n', sentence
  32.  
  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)
  39.  
  40.  
  41. def txt_conversor():
  42.     txt = replace_all(sentence, reps)
  43.     print '\n', txt
  44.  
  45. log = open('log.txt', 'w')
  46.  
  47. print(txt_conversor, file1=log)
  48.  
  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()
  53.  
  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
1 1940
sicarie
4,677 Recognized Expert Moderator Specialist
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)
  6.  
Jul 23 '14 #2

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

Similar topics

14
by: Jeffrey Barish | last post by:
Since upgrading to python 2.4 yesterday, I am no longer able to use Tkinter. From http://www.python.org/moin/TkInter I learned that I need to edit Modules/Setup to include Tkinter during the make....
3
by: Manish Gupta (BBS) | last post by:
Hello invoking something like following - sock.send(100) generates the following error - TypeError: send() argument 1 must be string or read-only buffer, not int
9
by: Alex | last post by:
I'm looking at a tutorial with the code below from wxPython.wx import * class MyApp(wxApp): def OnInit(self): frame = wxFrame(NULL, -1, "winApp", size = (800,640)) frame.Show(true)...
4
by: David Pratt | last post by:
Hi. Is anyone aware of any python based unacceptable language filter code to scan and detect bad language in text from uploads etc. Many thanks. David
1
by: Dave | last post by:
Hi All, I have a VS.NET 2003 solution that uses several setup projects to package various components and applications into MSI files. I need to provide foreign language support for 5-6 languages...
2
by: Calvin Lai | last post by:
Hi all, I have a serious problem with the application I am developing. The language I used are mainly English, and some Chinese. (But I guess this problem happens accross different language). ...
1
by: Krich | last post by:
I have text file contains word in English language and in foreign language and I include this text file as embedded resource in my project. VB.NET editor can display the foreign langue of this...
2
by: garabik-news-2005-05 | last post by:
I was playing with python encodings and noticed this: garabik@lancre:~$ python2.4 Python 2.4 (#2, Dec 3 2004, 17:59:05) on linux2 Type "help", "copyright", "credits" or "license" for more...
2
by: Alexandre Guimond | last post by:
Hi i was wondering if there already existed a simple python interpreter widget for tkinter? Basically, i would like to be able to lauch a python interpreter in a seperate window from my tkinter app...
6
by: Juha S. | last post by:
Hi, I'm writing a small text editor type application with Python 2.5 and Tkinter. I'm using the Tk text widget for input and output, and the problem is that when I try to save its contents to a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.