b=b+1
for a in range(0,b):
writefile = open("data.csv",'a')
writefile.write(str(name[a]) + "," + str(surname[a]) + "," + "\n")
writefile.close()
When I enter name and surname I only see the first letter on my file called data. Any idea why this is the case?
Thanks.
There are many things you should/could do differently style-wise but as there is no indication what b
, name
or surname
are there is very little help to be provided.
def calculate_check():
#receiving the Optionmenu values
child_s=int(child.get())
adult_s=int(adult.get())
place=str(flight_dst.get())
name=str(name_sv.get())
This is the portion of the code. Don't know if it will help.
Thanks.
surname=str(surname_sv.get())
I am afraid that when you fix this problem you will encounter several new ones.
You current problem is that name and surname are strings and your write specific characters based on index. Dummy example:
>>> name = 'Joe'
>>> surname = 'Doe'
>>> for a in range(3):
... print(name[a] + ', ' + surname[a])
...
J, D
o, o
e, e
What could I do to write whole string rather than individual characters?
You should print whole string
>>> print(name + ', ' + surname)
Joe, Doe
If using 3.6 <= Python then I suggest to use f-strings:
>>> print(f'{name}, {surname}')
Joe, Doe
But I want to write it onto the file not print it out.
So write it to file... or print it to file:
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
(END)
As you can read print() function in Python supports a "file" argument, which specifies where the function should write a given object(s) to (default is sys.stdout). But one can specify file and do something along those lines:
with open('my_file.csv', 'a') as f:
print(f'{name}, {surname}', file=f)