开发者

appending a line break to a list

I want my program to make a CSV out of inputs. (I'm tallying the number of times a particular input appears I then want to read that in Excel.) This is just for my own use to help with my data entry, so only the form of the output matters. I'm a total amateur when it comes to code, so pardon my circuitous method for getting the program to loop.

SC= []
ender= 0
while ender == 0:
     Data = input('type in the letters:')
     if 'x' in Data:
          SC.append (1, )
     else: SC.append (0, )
     A = input('Done?:')    
     if 'done' in A:
         ender = 1
     else:quit
else:SC.append('/n')

print(SC)

This gives me something like (1, 0, 1, '/n')

What I need is (1, 0, 1, /n)

I've tried SC.开发者_JS百科append(//n) -shouldn't this give me the 'escaped' / I want?- It returns a syntax error that points to the first /

If I use SC.append(/n) I get the same syntax error pointing to the /

I've also tried using the various forms of raw strings to no avail.

The problem of trying to add these line breaks at the end of the list has been driving me crazy. I can't seem to get python to append a literal /n to the list. Any help would be much appreciated.


This is surely not the best way to do it.

I've only tried to make few edits on your code:

sc = []
while True:
    data = raw_input('type in the letters:')
    if 'x' in data:
        sc.append("1")
    else:
        sc.append("0")
    checkDone = raw_input('Done?:')
    if 'done' in checkDone:
        break
sc.append('\n')
print(sc)
scWithCommas = ",".join(sc)
print(scWithCommas)

Several improvements can be made:

  1. The if 'x' in data check means that if you type xerox, an 1 is appended. If you type peter, a 0 is appended on the list. It could be altered so xerox appends 1,0,0,0,1 and peter appends 0,0,0,0,0.

  2. check for done could avoid second input from user and use first input instead.


You're escaping the newline character wrong. It's \n (notice you typed /n)

So what you want is SC.append('\n')

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜