Backspace does not seem to work in python

ぃ、小莉子 提交于 2021-02-08 07:43:47

问题


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?


回答1:


Why not to use str(dict)?

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



回答2:


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.




回答3:


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()]



回答4:


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



回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/5886985/backspace-does-not-seem-to-work-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!