[Tutor] Write string into files

Brian van den Broek bvande at po-box.mcgill.ca
Wed Oct 6 10:42:00 CEST 2004


Ms Soo Chong said unto the world upon 2004-10-06 03:16:
> Hi,
> 
> 
> I'm trying to combine a bunch of data from a form
> into a string and write it into a file. It works
> fine if the user input each and every fields but it
> showed the following error if the user did not. And 
> these fields are not compulsory, they may or may not 
> have values. So how should I do it? Btw, the way I'm
> doing it now is kinda of longwinded so if anyone can
> suggest me a better deal of grabbing all the data
> into a file straight, it would be very nice.
> 
> Any help is greatly appreciated.
> 
> Thank you.
> 
> Shufen
> 
> #####################################################
> A problem occurred in a Python script. Here is the
> sequence of function calls leading up to the error,
> in the order they occurred.
> 
>  /var/www/cgi-bin/sf/run_des.py
>   192         str += "<blame>" + blame + "</blame>"
> + "\n"
>   193         str += "<date_string>" + date_string +
> "</date_string>" + "\n"
>   194         str += "<nozzle>" + nozzle +
> "</nozzle>" + "\n"
>   195         str += "<shocktube>" + shocktube +
> "</shocktube>" + "\n"
>   196         str += "<reservoir>" + reservoir +
> "</reservoir>" + "\n"
> str = '<facility_name>T4 Shock
> Tunnel</facility_name>\n<...F/PJ</blame>\n<date_string>05/10/04</date_string>\n',
> nozzle = None
> 
> TypeError: cannot concatenate 'str' and 'NoneType'
> objects
>       __doc__ = 'Inappropriate argument type.'
>       __getitem__ = <bound method
> TypeError.__getitem__ of <exceptions.TypeError
> instance at 0x81d31dc>>
>       __init__ = <bound method TypeError.__init__ of
> <exceptions.TypeError instance at 0x81d31dc>>
>       __module__ = 'exceptions'
>       __str__ = <bound method TypeError.__str__ of
> <exceptions.TypeError instance at 0x81d31dc>>
>       args = ("cannot concatenate 'str' and
> 'NoneType' objects",) 
> 
> ######################################################
>


Hi Shufen,

I'm still at a learning stage, and don't have the greatest record of
giving reliable answers, so take this all skeptically. (I find trying to
answer a good way to improve my understanding.) And, if I make a goof of
it, someone more helpful will be along to point out the error :-)

A quick fix might be to assign each name you are using as a name for a
form field to '' at the beginning, and afterwards let the real values
that do exist take the place of the empty string. That way, you will
always be concatenate strings to strings, even when you don't have real
data to concatenate.

But depending on your data structure, and processing needs, a dictionary
approach might work better still. You don't say much about what your
data looks like. I'm going to assume it has known delimiters for each
datapoint at the start of lines, each datapoint entirely on a line to
itself. Under that assumption, I'd try something like:

Make a list of the delimiting strings in the order you want them in your
output:

data_points = ['Name:', "Address:', 'Phone:', 'favourite colour:']

# Is there only one record per datafile? Assuming so

the_record = {}

for line in data_file:
     for dp in data_points:
     # if you know more about the structure, there may well be better
     # ways than looping over all items in data_points
         if line.startswith(dp):
             the_record[dp] = line(len(dp):-1]			
             # -1 for trailing '\n'. Might need to be len(dp) + 1, or
             # whatever your data format demands

# Then, to print use something like:

output_dps = [x for x in data_points if x in the_record.keys()]
               # if x in the_record works, too, but I like to remind
               # myself I;m talking about keys of a dictionary.
for i in output_dps:
     print i, the_record[i]
     # or whatever it is you want done with the data

I've been doing something similar with multi-record data files. This
requires the extra logic to detect the start of a new data set in the
file, begin a new dictionary for it, etc.

Since you don't say what you are doing, I can't tell if this would be 
better than what you have. But, depending on how close my assumptions 
are to your situation, this strategy might be adaptable. Anyway, HTH,

Brian vdB




More information about the Tutor mailing list