Connecting to remote Oracle db via Python
Hi Guys, I've installed the cx_Oracle module for Python and I'm trying to connect to my remote Oracle db. Like so (username, password and ip below aren't real don't worry) >>> uid = "scott" >>> pwd = "tiger" >>> service = "10.5.1.12:1521:PR10" >>> db = cx_Oracle.connect(uid + "/" + pwd + "@" + service) This however gives me the following error: Traceback (most recent call last): File "", line 1, in cx_Oracle.DatabaseError: ORA-12545: Connect failed because target host or object does not exist I've also tried the following (jdbc string which works fine for java) >>> service = "jdbc:oracle:thin:@10.5.1.12:1521:PR10" >>> db = cx_Oracle.connect(uid + "/" + pwd + "@" + service) Traceback (most recent call last): File "", line 1, in cx_Oracle.DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified I'm not sure what's going on because I know that the ip, port and service name. are correct? And as I said I can connect to it via JDBC in Java. Any ideas? Thanks, Paul -- http://mail.python.org/mailman/listinfo/python-list
logging.handlers.SMTPHandler and fileConfig
I'm trying to use pythons logging.handlers.SMTPHandler with a configuration file (so that I don't have to have passwords etc. inside of the script) Now the guide I'm following is [URL="http://docs.python.org/library/ logging.html#configuration-file-format"]here[/URL], now the RotatingFileHandler is working, but I never receive an email, or an error for the SMTPHandler. Anyway here's the python code import logging import logging.config logDir = "./logs/" logging.config.fileConfig(logDir+'logging.conf') logging.getLogger('email') logging.debug('THIS IS A DEBUG MESSAGE') logging.error('THIS IS AN ERROR') And here's the config file [loggers] keys=root,email [logger_root] level=DEBUG handlers=rotatingFileHandler [logger_email] level=ERROR handlers=email qualname=email [formatters] keys=emailFormatter,rotatingFormatter [formatter_emailFormatter] format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s [formatter_rotatingFormatter] format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s datefmt=%m-%d %H:%M [handlers] keys=email,rotatingFileHandler [handler_email] class=handlers.SMTPHandler level=ERROR formatter=emailFormatter args=('mail.xx.cocom','f...@abc.com',['t...@def.com',],'ERROR!', ('user','passwd')) [handler_rotatingFileHandler] class=handlers.RotatingFileHandler level=DEBUG formatter=rotatingFormatter args=('./logs/log.out', 'maxBytes=100', 'backupCount=5') Because I wasn't getting an error I decided to temporarily add some print statements into .\Lib\logging\handlers.py, In SMTPHandler __init__ I print out mailhost, from, to etc. And these are all correct. I then inserted a few print statements into the different levels of emit to see which branch of the logic it was following. None of the print statements print. Which leads me to believe emit() is never being called and therefore the email never gets sent. So what am I doing wrong? -- http://mail.python.org/mailman/listinfo/python-list
Using csv.DictReader with \r\n in the middle of fields
Hello everyone! Hopefully this will interest some, I have a csv file (can be downloaded from http://www.paulstathamphotography.co.uk/45.txt) which has five fields separated by ~ delimiters. To read this I've been using a csv.DictReader which works in 99% of the cases. Occasionally however the description field has errant \r\n characters in the middle of the record. This causes the reader to assume it's a new record and try to read it. Here's the code I had import csv fields = ["PROGTITLE", "SUBTITLE", "EPISODE", "DESCRIPTION", "DATE"] delim= '~' lineReader = csv.DictReader(open('45.txt', 'rbU'), delimiter=delim,fieldnames=fields) def FormatDate(date): return date[6:10] +"-" +date[3:5] + "-" +date[0:2] channelPrograms = [] for row in lineReader: row["DATE"] = FormatDate(row["DATE"]) channelPrograms.append(row) Which when run would give me an error as it was trying to pass a NoneType to the FormatDate method, which obviously couldn't handle it. I'd like to find a way to read this record correctly despite the \r \n's in the middle of the description. The problem is I can't change the behaviour in which it reads a record. For the moment I've had to resort to extending the csv.DictReader and overriding the next() method to set the number of fields versus the number of values, if they're not equal I don't add those lines to my list of records. import csv class ChanDictReader(csv.DictReader): def __init__(self, f, fieldnames=None, restkey=None, restval=None, dialect="excel", *args, **kwds): csv.DictReader.__init__(self, f, fieldnames, restkey, restval, dialect, *args, **kwds) self.lf = 0 self.lr = 0 def next(self): if self.line_num == 0: # Used only for its side effect. self.fieldnames row = self.reader.next() self.line_num = self.reader.line_num # unlike the basic reader, we prefer not to return blanks, # because we will typically wind up with a dict full of None # values while row == []: row = self.reader.next() d = dict(zip(self.fieldnames, row)) self.lf = len(self.fieldnames) self.lr = len(row) if self.lf < self.lr: d[self.restkey] = row[self.lf:] elif self.lf > self.lr: for key in self.fieldnames[self.lr:]: d[key] = self.restval return d fields = ["PROGTITLE", "SUBTITLE", "EPISODE", "DESCRIPTION", "DATE"] delim= '~' lineReader = ChanDictReader(open('45.txt', 'rbU'), delimiter=delim,fieldnames=fields) def FormatDate(date): return date[6:10] +"-" +date[3:5] + "-" +date[0:2] channelPrograms = [] for row in lineReader: print "Number of fields: " + str(lineReader.lf) + " Number of values: " + str(lineReader.lr) if lineReader.lf == lineReader.lr: row["DATE"] = FormatDate(row["DATE"]) channelPrograms.append(row) Anyone have any ideas? :o) Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Using csv.DictReader with \r\n in the middle of fields
On Oct 13, 4:01 pm, Neil Cerutti wrote: > On 2010-10-13, pstatham wrote: > > > Hopefully this will interest some, I have a csv file (can be > > downloaded fromhttp://www.paulstathamphotography.co.uk/45.txt) which > > has five fields separated by ~ delimiters. To read this I've been > > using a csv.DictReader which works in 99% of the cases. Occasionally > > however the description field has errant \r\n characters in the middle > > of the record. This causes the reader to assume it's a new record and > > try to read it. > > Here's an alternative idea. Working with csv module for this job > is too difficult for me. ;) > > import re > > record_re = > "(?P.*?)~(?P.*?)~(?P.*?)~(?P.*?)~(?P.*?)\n(.*)" > > def parse_file(fname): > with open(fname) as f: > data = f.read() > m = re.match(record_re, data, flags=re.M | re.S) > while m: > yield m.groupdict() > m = re.match(record_re, m.group(6), flags=re.M | re.S) > > for record in parse_file('45.txt'): > print(record) > > -- > Neil Cerutti Thanks guys, I can't alter the source data. I wouldn't of considered regex, but it's a good idea as I can then define my own record structure instead of reader dictating to me what a record is. -- http://mail.python.org/mailman/listinfo/python-list