简体   繁体   中英

How can I make Python using csv read the first word in a line rather than the first character?

I've looked around for solutions to this here, and though they seemed to fit my needs they ended up not working for me. I'm not particularly sure why. I'll try and specify what the issue is and hopefully you'll be able to specify why this was different.

So, I've just been trying to make a simple username and password log-in system using a Tkinter GUI. I've tried making this work by putting the usernames and passwords in an external CSV file, and then having my program, once both users for my game that's already functioning have attempted to log-in, read line-by-line my csv file (called "Accepted Users for Card Game.csv"), which is in the same folder as the program so no further directory is necessary. I've tried making it so that the user puts their username and password into different Tkinter entries, and then presses a button beneath it. The program then checks if, for example, line[0] == PlayerOneUsername and line[1] == PlayerOnePassword , however every time I've tried this, it didn't work. (My delimiter is ',', for anybody wondering).

After making it print what it was reading, I've discovered that it always returns the first character of the line rather than the first word, so of course it keeps on being incorrect. I've searched for an answer, some of which I was already trying, however every time I've tried implementing it, the same issue seems to have been occurring. (EG line.split() hasn't fixed my problem).

This is the output on comparing the line and line[0] next to each other. (Ignore the colours and the title afterwards, those are to do with the game).

Here is the portion of my player two evaluation that triggers when you press the button (it's more or less identical to my player one evaluation):

Success["text"] = ""
if not PasswordLoginInputTwo.get() or not UsernameLoginInputTwo.get():
     Success["text"] = "One or more of your password/username entries are empty. Please try again."
else:
    csvfile = open ("Accepted Users for Card Game.csv", "r")
    Csvreader = csv.reader(csvfile, delimiter = ',')
    Success["text"] = "Player Two password/username combination has been read."
    for line in csvfile:
        print(line[0])
        if line:
            print(line)
            print(line[0])
            if line.split(None, 1)[0] == UsernameLoginInputTwo.get() and line.split(None, 1)[1] == PasswordLoginInputTwo.get():
                PlayerTwoSuccess = True
                PlayerTwoUsername = UsernameLoginInputTwo.get()
                PlayerTwoColour = line[2]
                PlayerTwoAntithesis = line[3]
                PlayerTwoVictorSound = "Victory - "+line[4]+".wav"
    PlayerTwoAttempts = PlayerTwoAttempts + 1

The external csv file is just arranged in a simple line format, like this:

PlayerOne,Password1,Purple,Yellow,Donkey Kong
PlayerTwo,Password2,Red,Green,Final Fantasy
PlayerThree,Password3,Pink,Purple,Worms
PlayerFour,Password4,Black,White,Team Fortress 2

I know the answer is probably rather obvious but I'm quite annoyed that I can't seem to find the answer! I'm using Python 2.7 by the way, though I don't intend to carry on using it. (As you can probably tell, I'm extremely new to Python, so please explain any solution like I'm extremely dumb - though you probably already got that impression from my code and the overall problem haha). Thanks!

So basically, to repeat, I am under the impression that line[0] should be the first 'word' in my 'line', in this case being for example "PlayerOne". Instead, it is giving me the first character, in this case, 'P'. How can I rectify this issue? What is causing this? How can I make it so it's the first word rather than the first character ? Thank you!

You are iterating over:

for line in csvfile:

Which will return a string representing the first line. You need to loop over the reader object:

for line in Csvreader:

Then, line will be a tuple of the strings split on the delimiter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM