By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

How to eliminate quotes around string field written to a file.

P: n/a
len
hi

Have this code in my program;

filename = 'custfile'
codeline = filename + ' = [\n'
output.write(codeline)

record written to file look like this

"custfile" = [

Yet in another part of the program I have code:

def fmtline(fieldline):
code = '(' + fieldline[1].replace('-', '_').replace('.',
'').lower() + \
', ' + fieldline[3] + '),\n'
return code
...
output.write(fmtline(wrkline))

fieldline is a list of which the first item in the list is a fieldname

record written to file looks like this, no quotes

(customer_no, X(6).),
(customer_name, X(30).),
(customer_street_1, X(30).),
(customer_street_2, X(30).),
(customer_city, X(15).),

Why quotes in the first case and not the second and how do I get read
of the quotes
in the first case.

Thanks
Len Sumnle
Nov 15 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Nov 15, 3:39*pm, len <lsumn...@gmail.comwrote:
hi

Have this code in my program;

* * filename = 'custfile'
* * codeline = filename + ' = [\n'
* * output.write(codeline)

record written to file look like this

* * *"custfile" = [
Assuming output is a file object, what you say is not possible. Here's
a demonstration that your code above produces a file without quotes
around custfile.

=== start demo ===
C:\junk>type foo.txt
The system cannot find the file specified.

C:\junk>type len_demo.py
output = open('foo.txt', 'w')
filename = 'custfile'
codeline = filename + ' = [\n'
print 'repr(codeline) is', repr(codeline)
output.write(codeline)

C:\junk>python len_demo.py
repr(codeline) is 'custfile = [\n'

C:\junk>type foo.txt
custfile = [

C:\junk>
=== end demo ===

I suggest that you remove the output file, and try your program again.
Check that you are not running some obsolete version of your program.
If this code is in an imported module, remove any related .pyc
and .pyo files before you start.

HTH,
John
Nov 15 '08 #2

P: n/a
On Fri, 14 Nov 2008 20:39:53 -0800, len wrote:
hi

Have this code in my program;

filename = 'custfile'
codeline = filename + ' = [\n'
output.write(codeline)

record written to file look like this

"custfile" = [
I cannot reproduce that behaviour. I suggest that the code you are
running is not the same as the code you say you are running.

--
Steven

Nov 15 '08 #3

P: n/a
On Sat, Nov 15, 2008 at 12:19 AM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
On Fri, 14 Nov 2008 20:39:53 -0800, len wrote:
>hi

Have this code in my program;

filename = 'custfile'
codeline = filename + ' = [\n'
output.write(codeline)

record written to file look like this

"custfile" = [

I cannot reproduce that behaviour. I suggest that the code you are
running is not the same as the code you say you are running.
--
Steven

Same here. I don't get quotes around it. Also, how is "output"
defined? I got an error, so I used sys.stderr.write() instead.
Nov 15 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.