jice 53 Posting Whiz in Training

my bad, the code i got is:

input.csv
1,2,text, date, qwertyuiopasdfghjklñzxcvbnm, yhnujmik,2121212121

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=',',quoting=csv.QUOTE_NONNUMERIC))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = row[0] + row[1] +  row[5] +  row[6] 
	csv_out.writerow( content )

which is giving me: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1 instead of 1,2,yhnujmik,2121212121

thanks!

Ok with Gribouillis.
Just to modify your code to make it work :

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=','))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = [row[0], row[1],  row[5],  row[6]]
	csv_out.writerow( content )
jice 53 Posting Whiz in Training

Another method

import fnmatch
pattern = '*.rtf'
files = ['doc.txt', 'stuff.rtf', 'artfunction.exe', 'rtfunc.bat', 'doc2.rtf']
print('\n'.join(filename for filename in fnmatch.filter(files, pattern)))
jice 53 Posting Whiz in Training

A classical method using list comprehensions

dict([(list[i], list[i+1]) for i in range(0, len(list), 2)])

function dict takes a list of tuples for arguments and uses the first term of the tuple as key and the second as value
If you need more explanations, don't hesitate to ask.

jice 53 Posting Whiz in Training

you have to do

folder = r'\\192.168.0.12\myshare1'
# OR
folder = '\\\\192.168.0.12\\myshare1'
jice 53 Posting Whiz in Training

Thanks for posting your result.
It's always useful to see the result of searching...

jice 53 Posting Whiz in Training

Something like this :

import random

class TV(object):
    def __init__(self): # self refers to the object itself
                        # Don't mix up an object instance and an object class.
                        # Class is the definition of a type of object (for example TVs all have a brand and channels)
                        # instance is one particular object (the tv with its serial number)
        tvslct=("40 inch LCD","70 inch 3D","15 inch old school")
        self.tv = random.choice(tvslct)  # self.something refers to the variable something affected to the object instance itself
        self.state = 0
        self.channel = 1
        print("You have been given a",self.tv,"Tv.")
        print("The Tv has been setup")

    def turnon(self):
        if self.state==1: # The state of the object instance
            print("The Tv is already on.")
        else:
            self.state=1
            print("The Tv has turned on.")
    def turnoff(self):
        if self.state==0:
            print("The Tv is already off.")
        else:
            self.state=0
            print("The Tv has turned off.")

    def changechannel(self, channel):
        if self.state==1:
            self.channel = channel # I affect the channel to the corresponding object instance variable
            print("It has changed.")
        else:
            print("The action could not be completed as the Tv is turned off.")

    def findchannel(self):
        # I don't undestand what you expect this to do so I can't correct...
        if state==1:
            prevchannels=channels
            channels=randint(0,15)
            while channels < prevchannels:
                randint(0,15)
            print("The total Tv channels has been updated.")
            print("The Tv now has",channels,"channels.")
        else:
            print("The action could not be completed as the Tv is turned off.")
        
my_tv = TV()               # I create a new tv and store it in a variable
                           # This is where the definitions stored in my class are instanciated …
jice 53 Posting Whiz in Training
import glob
print "\n\n".join(["".join([l for l in open(inf).readlines()[:2]]) for inf in glob.glob("C:\\path\\to\\the\\dir\\*.txt")])
jice 53 Posting Whiz in Training

oups 2

shutil.copy2(os.path.join(root, one_file), dst_subdir)
jice 53 Posting Whiz in Training

oups :

src_dir, patterns = line.strip().split(";")
jice 53 Posting Whiz in Training

First of all, I can't understand why you need to add

- app\source

as all the dirs you want to include are listed...

Anyway, i don't see how you can manage your need only with glob so i'll explain it with the old fashioned method I use.

First of all, unless you really need your dir list to be formatted as you posted it, i would do this another way :

On each line, you put the dir to be listed (those that don't need to be listed simply don't need to be in the file)
then, separated with some character (i'll use ";" because it's easier to see than tab) the patterns of your files you want to list (+) or not (-).
You'll never have a + and a - as if you choose to select only some files (+), those you want to exclude won't be in the list (normally)
Anyway, using this method, you can imagine to add other ';' and add other informations in it.

Note : For my very complicated backup scripts, i even use yaml ini files and put plenty of parameters to select very precisely the files i want to backup (drawback : the ini file is long to elaborate)

So here is the look of the file :

app\bin;-.txt,.log
app\docs
app\manuals;-.xls
app\source;+.py

Notice that in the first line lists 2 ext patterns separated by "," but this could be whatever you …

jice 53 Posting Whiz in Training

Hi,
You can use () to make it work (if you really want a one liner)...

def l():
    return (lambda ...)()
def l():
    return (lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x)))))()
jice 53 Posting Whiz in Training

You're right... glob is a very convenient way to deal with files and dirs.
But I'm so used to use os.walk and fnmatch that i hardly never use glob (but i should).
I'll come back on your questions tomorrow but what i can say about the last one is that you can use os.path.isdir(your_path) (see http://docs.python.org/library/os.path.html)
something like :

for one_line in open(my_file):
    my_glob = one_line.strip()
    if os.path.isdir(my_glob):
        my_glob = os.path.join(my_glob, "*.*")

The problem i can see with your way is that i don't think you can remove "*.txt" files using glob. This maybe one reason to use the old style method i use.
I'll look at that tomorrow

jice 53 Posting Whiz in Training

Can you put the code you use for writing the content of the chest (i suppose : chest.printChest()) and the datas that are written ?
BTW, for this kind of thing, you should have a look at __repr__ function.

jice 53 Posting Whiz in Training

Thank you for your feedback

jice 53 Posting Whiz in Training
print(["/".join(date.split("-"))])

2011/03/03

from my htc phone ;)

I'd rather do

date.replace('-','/')
jice 53 Posting Whiz in Training

If you're interested in generators (and you should be), here is the link that made me use generators and list comprehension:
http://www.dabeaz.com/generators/Generators.pdf

TrustyTony commented: Great reference on generators! +3
jice 53 Posting Whiz in Training

Sorry for this late reply...

Some comments :
1. The comment IN the function. This becomes a docstring and is used to document your programs.

2. I don't understand how this work :
"-" dirs are ignored so why are they in the file ?
Your file can contain only the dirs you want to copy so that you don't need to deal with the "+" and "-". Your code will be much simpler (you won't need a function to read the file)

Your function doesn't return anything... so what is the function used for ?

cpfolders only contains the last line so how do you want your main program to copy each dir ?
Note : This would be possible if your function is a generator but i don't think you ment it to be one...
Here is the code to make your function work as a generator (look for python generator on google to have more details)

myfile1 = "/home/user/locationoffile/filedirlist.txt"
# For myfile, i'd just affect the name to the variable and open it in the function.
# In your code, i can't see the closing of the file.
# When you do like I did, the closing is implicit at the end of the loop.

def readSource(myfile1):
    """This function reads the .txt file"""
    for line in open(myfile1):
        if line.startswith('-'):  #lines to ignore
            continue
        else:
            cpfolder = line.strip('+ \n') #strip the + and empty lines from the list …
jice 53 Posting Whiz in Training

an example of code

import os
import os.path
import shutil
import fnmatch

list_of_dirs_to_copy = ['path/to/dir/1', 'path/to/dir/2'] # List of source dirs
excluded_subdirs = ['dir1', 'dir2']  # subdir to exclude from copy
dest_dir = 'path/to/my/dest/dir'     # folder for the destination of the copy
files_patterns = ['*.txt', '*.doc']
for root_path in list_of_dirs_to_copy:
    for root, dirs, files in os.walk(root_path): # recurse walking
        for dir in excluded_subdirs:
            if dir in dirs:
                dirs.remove(dir)   # remove the dir from the subdirs to visit
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)  # create the dir if not exists
        for pattern in files_patterns:
            for thefile in fnmatch.filter(files, pattern):  # filter the files to copy
                shutil.copy2(os.path.join(root, thefile), dest_dir) #copy file
jice 53 Posting Whiz in Training

I'll give you more details tomorrow...
But it would be easier if you post some code you've done and precise the situation (what is the content of folder.txt file, which files are to be copied, which are not...)
As your question is general, i gave you general ideas you can use for your particular problem...
Anyway, you can easily google for examples of each of the functions I gave you.
They are not very difficult to understand.

jice 53 Posting Whiz in Training

Or, with a way to choose the length :

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"

length = 15

current = 0
for a in range(0, len(protein), 15):
    print "".join([str((i%100) / 10) for i in range(current, a)])
    print "".join([str(i % 10) for i in range(current, a)])
    print protein[current:a]
    print pp[current:a]
    print gor[current:a]
    print aber[current:a]
    print
    current = a
print "".join([str((i%100) / 10) for i in range(current, len(protein))])
print "".join([str(i % 10) for i in range(current, len(protein))])
print protein[current:len(protein)]
print pp[current:len(protein)]
print gor[current:len(protein)]
print aber[current:len(protein)]
jice 53 Posting Whiz in Training

You can try this :

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"


print "".join([str(i / 10) for i in range(len(protein))])
print "".join([str(i) for i in range(len(protein))])
print protein
print pp
print gor
print aber
jice 53 Posting Whiz in Training

Here are some function you can look at :
- os.listdir(mydir) will list all the files from "mydir"
- fnmatch.filter(files_list, "*.txt") will give you a list of all text files in the files_list (tip : fnmatch.filter(os.listdir(mydir))
- os.path.join(mydir, file) will give you the absolute name of the file
- os.walk(mydir) will list mydir and all its subdirs, giving you a tuple (root, dirs, files) where root is the current dir, dirs, the subdirs and files the files of the current dir. Tip : if you remove dir from dirs, it won't be visited.

jice 53 Posting Whiz in Training

Sorry, I thought i was writing python code ;-)
Thanks anyway, i'll look at ooo forums.

jice 53 Posting Whiz in Training

Nobody knows ?

jice 53 Posting Whiz in Training

Hello there,

I'm trying to generate a open office text document with tables generated from a database with the lpod library.
I'd like to set the width of the tables columns but i can't manage it.
Here is my test code so far :

import lpod.document
import lpod.table

doc = lpod.document.odf_new_document_from_type('text')
body = doc.get_body()

col_1_style = lpod.style.odf_create_style('table-column', 'column1', width='4cm')
doc.insert_style(col_1_style)

table = lpod.table.odf_create_table(u"Table 1", width=2, height=3)
for i in range(3):
    row = table.get_row(i)
    row.set_cell_values(range(2))
    table.set_row(i, row)

col = table.get_column(1)
col.set_column_style('column1')
table.set_column(1, col)

body.append(table)

doc.save("lpod_test.odt")

Does someone know how i am supposed to do to set the column width to 4 cm ?

Thanks for your help

jice 53 Posting Whiz in Training

So, the working code would be :

import mailbox

mbx=mailbox.mbox("./in_mbox")
mbx.lock()
of=open("out_mbox", "w")
for k, m in mbx.iteritems():
    of.write("From %s\n" % m.get_from())
    of.write(m.as_string())
mbx.unlock()
of.close()
jice 53 Posting Whiz in Training

By exploring the mailbox module code, i finally found what i was looking for.
It is the "get_from" function, which is also mentionned in the module documentation...
So, it works allright now.

I don't want to convert my mbox files because what i want to do is :
- automatically reorganize my mails (processed with thunderbird) by creating one folder per year
- automatically detach attached files and replace them with a html file to keep the link to reduce the mailbox size.

So, at the end, i want to have my mailbox with exactly the same format as before.

Thanks

Gribouillis commented: good! post your mbox parsing code when it's finished! +4
jice 53 Posting Whiz in Training

Also mbox is not a real format:
http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/mail-mbox-formats.html

Thanks for your links...
This one I knew but not the previous one.
In fact, with my little piece of code, i can read a thunderbird file and navigate through the mails. So, the most important is done.
My only problem is that i can't retrieve datas that are stored in the "From " line : the mailbox.mbox class recognise it but doesn't retrieve the datas.

As far as i can see, the mailbox.mbox and email.Message modules won't give me the answer so i'll look if i can overload the mbox class to deal with thunderbird's format or, if it's too complicated for me, i'll create a brand new "From " line that will have the same format (but not exactly the same datas).

Any help is welcome.
Thanks for having taken time to read my posts and help.

jice 53 Posting Whiz in Training

As it is generated by thunderbird, I have no way to change its format in my situation.
Except if i don't retrieve the "From " line and generate one myself (which should be ok but not very clean).

jice 53 Posting Whiz in Training

hi,
I try to write a app to automatically re-organize my mails (thunderbird).
The first thing I try is to re-create my folders tree, once for each year.
So i need to read mbox files and rewrite them

import mailbox

mbx=mailbox.mbox("./in_mbox")
mbx.lock()
of=open("out_mbox", "w")
for k, m in mbx.iteritems():
    of.write(m.as_string())
mbx.unlock()
of.close()

My problem is that during this process, I lose the "From " line between original file mails

in_mbox :

From - Mon Jun 16 08:54:05 2008
X-Account-Key: account2
X-UIDL: 919-1206101190
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-path: <adress@prv.com>
Received: from ...

out_mbox :

X-Account-Key: account2
X-UIDL: 919-1206101190
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-path: <adresse@prv.fr>
Received: from ...

"From " line is missing.

I try to get it with everything i could in email.Message object (get_all, get_unixfrom...) but i couldn't find the solution.

Does anyone know what i have missed ?
Thanks.

jice 53 Posting Whiz in Training

You can generate csv files...
For an easy csv management, there is a csv module
http://docs.python.org/library/csv.html

jice 53 Posting Whiz in Training

This should work (not tested)

of = open(my_out_filename, 'w')
for line in open(my_in_filename,'r'):
    if 'STRING' in line or 'INTEGER' in line:
        of.write(line)
of.close()
jice 53 Posting Whiz in Training

It is just a string formatting... You can use the % operator

jice 53 Posting Whiz in Training
jice 53 Posting Whiz in Training

Are you kidding ?
It's just the example I gave 5 post ago !

jice 53 Posting Whiz in Training

Have you tried the examples we gave you ?
Did you simply read them carefully ?
To me, all examples given allow what you've asked for !

jice 53 Posting Whiz in Training

My 2 cents
To use csv with other dialect and use dict(zip()) instead of named tuple.

"""
datas.csv :
"123"; "gishi"; "gishi@mymail.com"; "456 happy st."
"345"; "tony"; "tony.veijalainen@somewhere.com"; "Espoo Finland"
"""
import csv
class excel_french(csv.Dialect):
    delimiter=';'
    quotechar='"'
    doublequote=True
    skipinitialspace=False
    lineterminator='\n'
    quoting=csv.QUOTE_MINIMAL

csv.register_dialect('excel_french', excel_french)

header=['id', 'name', 'email', 'homeaddress']
d={}
for row in csv.reader(open('datas.csv'), 'excel_french'):
    drow=dict(zip(header, row))
    d[drow['id']]=drow
print d

>>>
{'123': {'email': ' "gishi@mymail.com"',
         'homeaddress': ' "456 happy st."',
         'id': '123',
         'name': ' "gishi"'},
 '345': {'email': ' "tony.veijalainen@somewhere.com"',
         'homeaddress': ' "Espoo Finland"',
         'id': '345',
         'name': ' "tony"'}}

If the first row of the file is the header line :

"""
datas.csv :
"id"; "name"; "email"; "homeaddress"
"123"; "gishi"; "gishi@mymail.com"; "456 happy st."
"345"; "tony"; "tony.veijalainen@somewhere.com"; "Espoo Finland"
"""
import csv
class excel_french(csv.Dialect):
    delimiter=';'
    quotechar='"'
    doublequote=True
    skipinitialspace=False
    lineterminator='\n'
    quoting=csv.QUOTE_MINIMAL

csv.register_dialect('excel_french', excel_french)

d={}
for i, row in enumerate(csv.reader(open('datas.csv'), 'excel_french')):
    if i==0:
        header=row
    else:
        drow=dict(zip(header, row))
        d[drow['id']]=drow
print d
jice 53 Posting Whiz in Training
import csv

reader = csv.reader(open("c:\sample.dat"))

d={}
for row in reader:
    d[row[0]]=row[1:]
jice 53 Posting Whiz in Training

sorry

print o

> my name is toto
jice 53 Posting Whiz in Training

Just a quick point.
To turn an object into string, you can define the special method __repr__ in your object :

class myobject:
    def __init__(self, name):
        self.name=name

    def __repr__(self):
        return "my name is %s" % (self.name)

o=myobject("toto")

print o

> toto
jice 53 Posting Whiz in Training

To take only links which contain products from your (not CSV) data:

for x in open('linklist.txt').readlines():
    _,_,link=x.partition(' ==> ')
    if 'products' in link: print link

Why do you "readlines()" ?
As far as i know, this loads the whole file in memory which is of no use...

for x in open('linklist.txt'):
    _,_,link=x.partition(' ==> ')
    if 'products' in link: print link

would do exactly the same

jice 53 Posting Whiz in Training

Your datas aren't in a csv format...
For csv, here is a simple example to read and write datas

import csv

# This class is to define another csv format if you need to
class excel_french(csv.Dialect):
    delimiter=';'
    quotechar='"'
    doublequote=True
    skipinitialspace=False
    lineterminator='\n'
    quoting=csv.QUOTE_MINIMAL

fic='yourfile.csv'
outcsvfic='out.csv'

csv.register_dialect('excel_french', excel_french)

cr=csv.reader(open(fic,"r"), 'excel_french') # 'excel_french' is optionnal.
            # Only if you want to use another csv format than the default one
cw=csv.writer(open(outcsvfic,'w'), 'excel_french')

for row in cr:
    print row
    cw.writerow(row[:2])

More informations in the doc
http://docs.python.org/library/csv.html

jice 53 Posting Whiz in Training

don't forget that, in os.walk, if you remove a dir from "dirs", it's subtree won't be explored.
So

for dirpath, dirs, files in os.walk(path):
    for d in dirs:
        if d.startswith('.'):
            dirs.remove(d)  # d's subtree won't be explored
    for f in files:
        if not f.startswith('.'):
            process_file()
jice 53 Posting Whiz in Training

A better way to deal with text files is rather with for loop :

of=open(myoutfilename, 'w')
for line in open(myfilename,'r'):
    print line
    of.write(line.replace(' ', '\t'))
of.close()

######### OR ##############
# not sure the 2 with on the same line work (and i can't test it right now)
# this would be a preferred way (even if i use the first one)

with open(myfilename,'r') as f, with open(myoutfilename,'w') as of:
    for line in f:
        print line
        of.write(line.replace(' ', '\t'))

if replacing the spaces is enough in your case

jice 53 Posting Whiz in Training

it's not tennisPro, it's tennis

jice 53 Posting Whiz in Training

It's because your class isn't "tennisPro", it's "tennis"

jice 53 Posting Whiz in Training

wherever you write :

something("%s") % one_variable

write

something("%s" % one_variable)
jice 53 Posting Whiz in Training

Just one thing. For line iterating, i find it is a bad idea to load the whole file in a list (using readlines()) except if you can't process it sequentially. This will consume memory for really nothing interesting...
Python allows iterating directly on file lines :

for line in open(myfile,'r'):
    print line

Or

with open(myfile,'r') as infile:
    for line in infile:
        print line

This seems to be the preferred way for some obscures reasons I don't know (if someone can explain, i would learn this with pleasure).

jice 53 Posting Whiz in Training

please put [ code] [ /code] tags around your code...
I still don't understand the problem...
I know neither what your logger is nor how and where you want to write in your logfile.
To determine the name of your logfile, you can do something like this

def cpi_subsql(args):
    try:
        loginfo = main._initlogger()
        logger = logging.getLogger(loginfo)
        if args == "":
            args = raw_input('Please Enter Sql file name:')
            if args == "":
                sys.exit

        # determine logfile name
        # Here, i take the sql file name, replacing the .sql with a .log
        logfilename="%s.log" % args[:-4]
        # but as I don't know how you wish to use it, i can't do anything more...
        cpi_run_sql.cpi_runsql(args)
    except:
        logger.exception("Error while executing cpi_sub_sql")
        raise
jice 53 Posting Whiz in Training

Fast method, using sets :

lines=open(myfile,'r').readlines()
uniquelines=set(lines)
open(outfile,'w').writelines(uniquelines)

which can be done in only one line :

open(outfile,'w').writelines(set(open(myfile,'r').readlines()))