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:
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
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()
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
Comment