Python Forum
Only first letter in the cell of the file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Only first letter in the cell of the file.
#1
            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.
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
    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())
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
What could I do to write whole string rather than individual characters?
Reply
#6
You should print whole string Smile

>>> print(name + ', ' + surname)
Joe, Doe
If using 3.6 <= Python then I suggest to use f-strings:

>>> print(f'{name}, {surname}')
Joe, Doe
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
But I want to write it onto the file not print it out.
Reply
#8
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get cell name from XML file paulo79 4 1,364 Mar-11-2022, 10:33 AM
Last Post: paulo79
  Python not reading cell in excel file wendysling 1 2,149 May-21-2019, 10:40 PM
Last Post: Larz60+
  [Help] How to count Letter frequency in a text file? vanicci 6 15,702 Aug-08-2018, 12:23 PM
Last Post: vanicci
  Problem with assigning directory of a file based on cell name mmaz67 3 2,820 Jul-04-2018, 08:40 AM
Last Post: Larz60+
  find cell value with matching regular expression of a row in excel file hruday 4 30,910 Jul-05-2017, 01:02 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020