Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Program:
Python
userr_name=input("Enter your user name that you want to create:")
passwordd=input("Enter the strong password:")
card_balance=int(input("Enter the amount to be stored:"))
column_names = ["USERNAME","PASSWORD","CARD_BALANCE","PENDING_AMOUNT"]

rows = [[userr_name],[passwordd],[card_balance],[0]]

with open('dataframe.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(column_names)
    for row in rows:
        writer.writerow(row)


What I have tried:

This is the Output:

OUTPUT:
USERNAME,PASSWORD,CARD_BALANCE,PENDING_AMOUNT
kanish
1234
1000
0
Posted
Updated 9-Feb-23 5:22am

Your rows data contains four rows: the 1st holds teh username, the second the password, and so on.
If you want them all on the same row - and you do - then you need to make them a single row:
Python
rows = [[userr_name, passwordd, card_balance, 0]]
 
Share this answer
 
Python
for row in rows:
    writer.writerow(row)

That writes each element of rows on a separate line; it should be:
Python
writer.writerow(rows)

Also you do not need all those extra square brackets, the list should simply be:
Python
rows = [userr_name,passwordd,card_balance,0]


[EDIT]
My modified code:
Python
import csv

userr_name=input("Enter your user name that you want to create:")
passwordd=input("Enter the strong password:")
card_balance=int(input("Enter the amount to be stored:"))
column_names = ["USERNAME","PASSWORD","CARD_BALANCE","PENDING_AMOUNT"]

rows = [userr_name,passwordd,card_balance,0]

with open('dataframe.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(column_names)
    writer.writerow(rows)

And the results:
C:\Users\rjmac\Documents\VSCode\Python>python cp.py
Enter your user name that you want to create:Richard
Enter the strong password:password123
Enter the amount to be stored:45

C:\Users\rjmac\Documents\VSCode\Python>type dataframe.csv
USERNAME,PASSWORD,CARD_BALANCE,PENDING_AMOUNT
Richard,password123,45,0

[/EDIT]
 
Share this answer
 
v2
Comments
OriginalGriff 9-Feb-23 12:17pm    
Quote:Also you do not need all those extra square brackets, the list should simply be:
rows = [userr_name,passwordd,card_balance,0]

Won't that give him the same results he has at the moment - one item per row?
Richard MacCutchan 9-Feb-23 12:29pm    
No, see my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900