简体   繁体   中英

Python loops with rpy and psycopg2

Good day,

I've been struggling some time now and cannot find an answer on Google. Im using Python with rpy (not rpy2) and psycopg2 for PostgreSQL. When trying a loop for SQL I would do the following:

for i in xrange(1:11):
    script = "DELETE FROM table WHERE column = %d" % i
    cur.execute(script)

This works fine, but how can I do something similar with rpy? For example the following:

for i in xrange(1:11):
    test = "15 * %d" % i
    r('test')                # This does not work
    r.assign('test', test')
    r('print(test)')         # This does not work either
    x = r('test')
    print x                  # This prints 15 * 1, then 15 * 2, but not 15 and 30.

Sorry if this is too simple, but I'm stuck!

I managed to figure it out!

Simply:

r('test <- 15 * %d' % i)

This never worked before as I did this:

r('test <- 15 * %d') % i

Not sure why this makes a difference, but this seems to work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM