开发者

Backspace does not seem to work in python

开发者 https://www.devze.com 2023-03-03 12:08 出处:网络
network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]} str1=\'network.csv\'
network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]}
str1='network.csv'
output = open(str1,'w')
for ii1 in network.keys():
    output.write(repr(ii1)+":[")
    for n in network[ii1]:
        output.write(' %s,'%(repr(n)))
    output.write('\b'+']\n')
output.close()

What I expect is something like:

1:[ 2, 3, 4]
2:[ 1, 3, 4]
3:[ 1, 2]
4:[ 1, 3, 5]
5:[ 6, 7, 8]
6:[ 5, 8]
7:[ 5, 6]
8:[ 5, 6, 7]

but what I get is:

1:[ 2, 3, 4,]
2:[ 1, 3, 4,]
3:[ 1, 2,]
4:[ 1, 3, 5,开发者_运维问答]
5:[ 6, 7, 8,]
6:[ 5, 8,]
7:[ 5, 6,]
8:[ 5, 6, 7,]

I am a newbie....could someone please help?


The "\b" simply inserts the ASCII backspace character; it does not remove the just-written character from the output file. This is why your code doesn't behave as you expect.

Now, to fix it you could replace

for ii1 in network.keys():
    output.write(repr(ii1)+":[")
    for n in network[ii1]:
        output.write(' %s,'%(repr(n)))
    output.write('\b'+']\n')

with

for ii1 in network.keys():
    output.write(repr(ii1)+":[ ")
    output.write(", ".join(map(repr, network[ii1])))
    output.write(']\n')

or, to improve it further, with

for k, v in network.items():
    print >>output, "%s:[ %s]" % (repr(k), ", ".join(map(repr, v)))

Lastly, if the keys are simple integers as your example indicates, then the repr(k) can be simplified to just k. Also, if the values in the dictionary are lists of integers or somesuch, then the entire ", ".join(map(repr, v)) dance might be unnecessary.


Use str.join to generate Comma-Separated-Values, to avoid the need for backspace:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

A simpler approach is, for example, list comprehensions iterating over dictionary items:

>>> [output.write("%s:%s\n" % item) for item in network.items()]


Why not to use str(dict)?

for k, v in network.iteritems():
    output.write(str({k: v})[1:-1] + '\n')


You can't delete characters written in a file in general.

However, with a little redesigning of your code, you can get this:

network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]}
str1='network.csv'
output = open(str1,'w')
for ii1 in network.keys():
    output.write(repr(ii1)+":[")
    first=false
    for n in network[ii1]:
        if first: 
             first=false
        else:
             output.write(',')
        output.write('%s'%(repr(n)))
    output.write('\b'+']\n')
output.close()


Whether or not the backspace character actually 'backspaces' is probably dependent on the shell you're using.

It is much simpler and easier (and proper) to just output the data yourself as you want it formatted.

network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]}
output = open('network.csv','w')
for key,values in network.items():
    str_values = [str(x) for x in values]
    output.write('%s:[%s]' % (key,','.join(str_values))
output.close()


Try this:

network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]}
str1='network.csv'
with open(str1, 'w') as output:
    for ii1 in network.keys():
        output.write(repr(ii1)+":[")
        output.write(','.join(repr(n) for n in network[ii1]))
        output.write(']\n')

Output in network.csv:

1:[2,3,4]
2:[1,3,4]
3:[1,2]
4:[1,3,5]
5:[6,7,8]
6:[5,8]
7:[5,6]
8:[5,6,7]

Some points:

  • I'm using with ... as ...:. This guarantees that the file will be closed properly.

  • I'm using ','.join to create the comma-separated list. This is the 'pythonic' way to merge lists (or, more precisely, iterables) of strings.

0

精彩评论

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