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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • len

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

    hi

    Have this code in my program;

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

    record written to file look like this

    "custfile" = [

    Yet in another part of the program I have code:

    def fmtline(fieldli ne):
    code = '(' + fieldline[1].replace('-', '_').replace('. ',
    '').lower() + \
    ', ' + fieldline[3] + '),\n'
    return code
    ...
    output.write(fm tline(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_stree t_1, X(30).),
    (customer_stree t_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
  • John Machin

    #2
    Re: How to eliminate quotes around string field written to a file.

    On Nov 15, 3:39 pm, len <lsumn...@gmail .comwrote:
    hi
    >
    Have this code in my program;
    >
        filename = 'custfile'
        codeline = filename + ' = [\n'
        output.write(co deline)
    >
    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(co deline)

    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

    Comment

    • Steven D'Aprano

      #3
      Re: How to eliminate quotes around string field written to a file.

      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(co deline)
      >
      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

      Comment

      • Shawn Milochik

        #4
        Re: How to eliminate quotes around string field written to a file.

        On Sat, Nov 15, 2008 at 12:19 AM, Steven D'Aprano
        <steve@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(co deline)
        >>
        >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.writ e() instead.

        Comment

        Working...