[Tutor] Searching for time in a given string

2010-04-01 Thread Judith Flores
Hello,

   I was wondering if someone could provide me with the pattern syntax to find 
the time in a given string. The time usually appears as "14:28:32" (for 
example) within a string ("Sun Jan 23 14:28:32 1965"). How can I retrieve the 
time?


Thank you very much in advance for your help,

Judith


  

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Difference in minutes between two time stamps

2009-03-02 Thread Judith Flores

Hello,

   I can't seem to figure out the syntax to calculate the difference in minutes 
between two time stamps. I already read the documentation about datetime and 
time modules, but I was unable to implement the code.

My code will be fed with two timestamps (as styrings):

start="09:35:23"
end="10:23:00"

Could someone guide me on how to calculate the difference in minutes 
between both stamps?

Your help is very much appreciated.

Thank you,

Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to avoid error message: "_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?"

2009-02-06 Thread Judith Flores
Hello,

   I get the error message above quite often when trying to open some csv 
files. I noticed that if I open the csv file in excel and delete the "empty" 
columns at the right end of my data, the error message doesn't appear.

My code looks like this:

myfile=open(csvfile)
reader=csv.reader(myfile)
header=reader.next()

Thank you,

Judith

Let me know if you would like to have some of my csv files.



  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Writing long strings (that include commas) to a csv file

2009-01-22 Thread Judith Flores
Hello dear Python experts,

How can I write a complete string (which sometimes will include many commas) to 
a cell in a CSV file? With the code below, what I obtain is a cell per letter 
of every string:


file1=open('myfile.csv','w')
writer=csv.writer(file1)

mylist=[ " first , txt+words , more" , " second, text + words, and more"]
listnumbers=["0","5"]

for i in range(0,len(mylist)):

writer.writerow(mylist[i] + listnumbers[i])


file1.close()


So the first element of mylist should fill up only one cell in the csv file, 
and the same for the second elemente, and so on.

Any help would be very much appreciated.

Thank you!

Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Modify a dictionary using csv.DictWriter

2009-01-12 Thread Judith Flores
Hello,

   I have been struggling a while trying to figure out how to modify a csv file 
using the csv.DictWriter class. Let's suppose I have the following code:

import csv
outfile=open('template.csv','w') # This is a pre-existing file that 
contains 3 variables (3 columns). The variables will (or will not) have a value 
each.

x='new_value'  

writing=csv.DictWriter(outfile, 
fieldnames=['variable1','variable2','variable2'])

data={'variable1' : x}   # What I intent to do here is to overwrite whatever 
value exists for variable 1. There will always exist only one value per 
variable.

writing.writerow(data)

I have several questions about how DictWriter works, and I am sorry if my 
questions have a very obvious answer, which I am 'obviously' missing.


1. When I run the code above for the first time, the contents of the 
pre-existing file disappear, if I run the script a second time, now I can see 
the value of x.
2. The fieldnames don't appear in the csv file, only the values. 
3. Is there a way to let the DictWriter know that the header of the csv file 
corresponds to all the fieldnames? To avoid typing all the names.


As usual, thank you very much for your help.


Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Making a dictionary of dictionaries from csv file

2008-12-02 Thread Judith Flores
Dear Python community,

   I have been trying to create a dictionary of dictionaries (and more 
dictionaries) from a csv file. The csv file contains longitudinal data 
corresponding to names. The following is just a very (very) simple example of 
how the data looks:

NameDayweighttemp
name114537
name135536
name215936
name233436.5
name316637
name338736.8


So far I have written this:

from csv import *

f=open('myfile.csv',"rt")

row={}
maindict={}

reader=DictReader(f)

for row in reader:
maindict[row['Name']=row


then I can access the weight of a given name like this:

wg=int(maindict[['name1']['weight'])



My question is the following:

How can I convert the csv to a dictionary that would have the following 
structure?

maindict = {

'name1' : { 
'Day' : {

1 : { 'weight' : '45', 'temp' : '37' } ,

3 : { 'weight' : '55', 'temp' : '36' }

  }

},

'name2' : { . # and we repeat the same structure for the rest of the names.



}

 
>From my code above you can of course guess that it doesn't make beyond the 
>level of name.

Thank you very much,

Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Subprocess module

2008-11-26 Thread Judith Flores
Hello,

   A couple of weeks ago I posted a question about what documentation I should 
read in order to implement a way to communicate Python and R. I read about the 
module 'subprocess', but have not been able to do something very simple. I was 
wondering if you could tell me how to transfer a Python variable to R. For 
example, a variable called 'fileforR'  will acquire a pathfile from the user 
through a GUI. This file will be a csv file that will be processed in a R 
script for statistical analysis. Let's suppose the R script is called: 
'read_file.R', and that it contains the following code:

read.csv(fileforR)
q()

In the Python interpreter:

import easygui
import subprocess

fileforR = fileopenbox()

After this, I have tried different combinations to try to call R and the 
mentioned R script, for example:

subprocess.call('R')

opens the R console, but I can't access the R script. And there's another 
problem I can't solve: passing the 'fileforR' variable from Python to the R 
environment.


   I am working on SUSE Linux, and using Python 2.5.2.


Thank you very much,

Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Documentation on how to run complete R scripts from Python interpreter

2008-11-12 Thread Judith Flores
Hello,

   I am very new to Python and I have long R scripts that I would like to run 
under the Python Interpreter. The reason for this is because I will try to 
construct a GUI using 'easygui' in Python. I read some of the the documentation 
regarding the RPy module, but it implies to add a 'r' at the beginning of every 
R function, plus the dot to underscore conversion in the names of the R 
functions. Or maybe, what I am intending to do is not possible? I originally 
built a GUI using Rtcltk, but easygui honors its name, and I need to add some 
text manipulation also, which is much more intuitive to do in Python than R.



This is a diagram of what I would like to do:

GUI constructed with "easygui" --> this will retrieve pathfiles and user's 
preferences (floating numbers and strings) --> The preceeding attributes will 
feed the R scripts-->R scripts will generate pdf files with plots.

All this within the Python Interpreter, although I am suspecting that like the 
name indicates, this is only a 'Python' interpreter, nothing else...

   If you could orient me to where I can find documentation on what I would 
like to do, I would appreciate it very much.

Thank you,

Judith


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor