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')
(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 /
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:
The
if 'x' in data
check means that if you typexerox
, an 1 is appended. If you typepeter
, a 0 is appended on the list. It could be altered soxerox
appends 1,0,0,0,1 andpeter
appends0,0,0,0,0
.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')
精彩评论