开发者

python list substring

I am trying to read the variables from newreg.py (e.g. state, district, dcode, etc, a long list which in turn picking up data from a web form) into insertNew.py.

I have currently read the whole file into a list named 'lines'. Now, how do I filter each variable (like- state, district, etc. approx 50-55 variables. This 'list' also has html code as I have read the whole web page into it) from list 'lines'?

Is there a better and efficient way to do it ?

Once I am able to read each variable, I need to concatenate these value ( convert into string) and insert into MongoDB.

Lastly when the data has been inserted into DB, 'home.py' page opens.

I am giving details so that a complete picture is available for some solution which can be given. I hope it I have been able to keep it simple as well as complete.

I want to loop over the list (sample below) and filter out the variables (before '=' sign values). The following is in 'newreg.py' :

state = form.getvalue('state','ERROR')

district = form.getvalue('district','ERROR')

dcode = form.getvalue('Dcode','ERROR')

I read a file / page into a list

 fp = open('/home/dev/wsgi-scripts/newreg.py','r')
 lines = fp.readlines()

so that I can create dictionary to insert into MongoDB.eg.

info = {'state' : state , 'district' : district, . . . . } {key : value } [value means --- which is the variable from the above list]

Thanks

but i am getting the following errors when i do

print getattr(newreg, 'state')

the error is

>>> print getattr(newreg, 'state')
Traceback (most recent call last):
  File "<stdin>", line 1, in module
AttributeError: 'module' object has no attribute 'state'

I also tried

>>> print newreg.state
Traceback (most recent call last):
  File "<stdin>", line 1, in module 
AttributeError: 'module' object has no attribute 'state'

This is how I added the module

>>> import os,sys
>>> sys.path.append('/home/dev/wsgi-scripts/')
&g开发者_JS百科t;>> import newreg
>>> newreg_vars = dir(newreg)
>>> print newreg_vars
['Connection', 'Handler', '__builtins__', '__doc__', '__file__', '__name__',  
 '__package__', 'application', 'cgi', 'datetime', 'os', 'sys', 'time']

Handler in the above list is a class in the following

#!/usr/bin/env python

import os, sys
import cgi
from pymongo import Connection
import datetime
import time

class Handler:
     def do(self, environ, start_response):
     form = cgi.FieldStorage(fp=environ['wsgi.input'],
                             environ=environ)

     state = form.getvalue('state','<font color="#FF0000">ERROR</font>')
     district = form.getvalue('district','<font color="#FF0000">ERROR</font>')
         dcode = form.getvalue('Dcode','<font color="#FF0000">ERROR</font>')


I am assuming you want to copy the variables from one Python module to another at runtime.

import newreg
newreg_vars = dir(newreg)
print newreg_vars

will print all of the attributes of the module "newreg".

To read the variables from the module:

print getattr(newreg, 'state')
print getattr(newreg, 'district')
print getattr(newreg, 'dcode')

or if you know the names of the attributes:

print newreg.state
print newreg.district
print newreg.dcode

To change the attributes into strings, use a list comprehension (or a generator):

newreg_strings = [str(item) for item in newreg_vars]

This will save you lots of effort, as you will not have to parse "newreg" as a text file with re.

As a side note: Type conversion is not concatenation (although concatenation may involve type conversion in some other programming languages).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜