开发者

Save a table as text

开发者 https://www.devze.com 2023-03-17 14:28 出处:网络
What i have right now: oname = open(\"1231.txt\",\"w\") def cC_(): d_=\"fahrenheit 开发者_StackOverflow中文版celsius\"

What i have right now:

oname = open("1231.txt","w")

def cC_():

          d_="   fahrenheit 开发者_StackOverflow中文版       celsius"
          print(d_)
          for i in range(-300,213,1):
                  c_=(i-32)*(5/9)
                  a_=str(i)+'°F'
                  b_=str(round(c_,3))+'°C'
                  print("%10s" % (a_), '        ',"%10s" % (b_))

oname.write(cC_())

oname.close()

My Question: i cannot get a table which like the function's print in text.

fahrenheit celsius

-300°F          -184.444°C
-299°F          -183.889°C
-298°F          -183.333°C
-297°F          -182.778°C
-296°F          -182.222°C
-295°F          -181.667°C
-294°F          -181.111°C
-293°F          -180.556°C
-292°F            -180.0°C

.........


def cC_(fout=sys.stdout):
   ...
  print(..., file=fout)
   ...

with open(..., 'w') as oname:
  cC_(oname)


Ignacio's answer works for python3, for python2, you should do ...

def cC_(fout=sys.stdout):
    ...
    print >> fout, ...

with open(..., 'w') as oname:
    cC_(oname)


You are writing functions return value, not the strings printed in function.

You should change print calls to oname.write.

with open("1231.txt","w") as oname:
    oname.write("   fahrenheit        celsius\n")
    for i in range(-300,213,1):
        c_=(i-32)*(5/9)
        a_=str(i)+'°F'
        b_=str(round(c_,3))+'°C'
        oname.write("".join(("%10s" % (a_), '        ',"%10s\n" % (b_))))

Edit:

There is a better way with using .format instead of str conversions(You can do it with your old % notation too.):

with open("1231.txt","w") as oname:
    oname.write(" fahrenheit       celsius\n")
    for i in range(-300,213,1):
        c=(i-32)*(5/9)
        oname.write("{0:<10.3f}°F    {1:<10.3f}°C \n".format(i, c))


I propose:

oname = open("1231.txt","w")

def cC_():
    return ("%13s%18s\n" % ('fahrenheit','celsius') \
            + '\n'.join("%10s °F %14s °C" % ( i, '%.3f' % ((i-32)*5./9) )
                        for i in xrange(-300,213,1) ) )

print cC_()
oname.write(cC_())

oname.close()

It's written with the ancient string formatting tool. It is certainly possible to change this code to use the format() built-in function.

   fahrenheit           celsius
      -300 °F       -184.444 °C
      -299 °F       -183.889 °C
      -298 °F       -183.333 °C
      -297 °F       -182.778 °C
      -296 °F       -182.222 °C
      -295 °F       -181.667 °C
      -294 °F       -181.111 °C
      -293 °F       -180.556 °C
      ...........
      -149 °F       -100.556 °C
      -148 °F       -100.000 °C
      -147 °F        -99.444 °C
      -146 °F        -98.889 °C
      -145 °F        -98.333 °C
      -144 °F        -97.778 °C
      ...........
        12 °F        -11.111 °C
        13 °F        -10.556 °C
        14 °F        -10.000 °C
        15 °F         -9.444 °C
        16 °F         -8.889 °C
        17 °F         -8.333 °C
        18 °F         -7.778 °C
        19 °F         -7.222 °C
        20 °F         -6.667 °C
        21 °F         -6.111 °C
        22 °F         -5.556 °C
        23 °F         -5.000 °C
        24 °F         -4.444 °C
        25 °F         -3.889 °C
        26 °F         -3.333 °C
        27 °F         -2.778 °C
        28 °F         -2.222 °C
        29 °F         -1.667 °C
        30 °F         -1.111 °C
        31 °F         -0.556 °C
        32 °F          0.000 °C
        33 °F          0.556 °C
        34 °F          1.111 °C
        35 °F          1.667 °C
        36 °F          2.222 °C
        37 °F          2.778 °C
        38 °F          3.333 °C
        39 °F          3.889 °C
        40 °F          4.444 °C
        41 °F          5.000 °C
        42 °F          5.556 °C
        43 °F          6.111 °C
        44 °F          6.667 °C
        45 °F          7.222 °C
        46 °F          7.778 °C
        47 °F          8.333 °C
        48 °F          8.889 °C
        49 °F          9.444 °C
        50 °F         10.000 °C
        51 °F         10.556 °C
        52 °F         11.111 °C
        53 °F         11.667 °C
        ...........
        .......
0

精彩评论

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