尝试写入时出现CSV错误

时间:2016-02-28 21:53:26

标签: python python-3.x

import csv

name = input(': ')
password = input(': ')
age = input(': ')


hello = [name, password, age]
length = len(hello[0])

with open('db.csv', 'a') as testfile:
    csv_writer = csv.writer(testfile)
    for y in range(length):
        csv_writer.writerow([x[y] for x in hello])

当我单独在一个单独的python文件上运行上面的代码时,它可以工作,但每当我尝试将它放在我的完整代码中时,它都不起作用。 我想要做的是基本上做一个寄存器,当我输入它写入csv文件。我还添加了验证码验证,因为为什么不。

csv_writer.writerow([x[y] for x in hello]):

第33行^ 完整的代码v

import random
import csv

def reg2():
    print('wip')

def reg1():
    ok = False
    while not ok:
        try:
            name = input('Enter your name: ')
            age = int(input('Enter your age:'))
            password = input('Enter your password: ')
            confirm = input('Confirm Password: ')
            if confirm == password:
                alphabet =''.join(random.choice('0PQRSTUVefghij56789WXYZabcdABCDEFCDrstuvwEFGJ234NOKLMkHImnopqxyz') for i in range(7))
                print(alphabet)
                captcha = input('Enter the words shown above (Not Case Sensitive): ')
                if captcha.capitalize() == alphabet.capitalize() or 'admin'.capitalize():
                    print('Name: ' + name)
                    print('Age: {}'.format(age))
                    print('Password: ' + password)
                    option = input('Enter No to register again. Enter Yes to login.')
                    if option.startswith('N') or option.startswith('n'):
                        reg1()
                    elif option.startswith('Y') or option.startswith('y'):
                        hello = [name, password, age]
                        length = len(hello[0])

                        with open('db.csv', 'a') as testfile:
                            csv_writer = csv.writer(testfile)
                            for y in range(length):
                                csv_writer.writerow([x[y] for x in hello])
                else:
                    captcha = input('Try again: ')
                    if captcha == alphabet:
                        print('Confirmed. ')
                    else:
                        print('You have tried too many times. Please register again.')
                        reg1()
            else:
                print('Your password is incorrect. Please register again.')
        except ValueError:
            print('Error 666: Please register again.')
reg1()

我该如何解决这个问题?

完整的追溯错误是http://pastebin.com/zshHji8i

2 个答案:

答案 0 :(得分:0)

[...]
length = len(hello[0])

with open('db.csv', 'a') as testfile:
    csv_writer = csv.writer(testfile)
    for y in range(length):
        csv_writer.writerow([x[y] for x in hello])

所以在这里,“长度”是name变量中的字符数,后面几行,for y in range(length)你“枚举”字母,说你有6个字母你是在y获得0,1,2,3,4,5。一行后来[x[y] for x in hello]你要求y的{​​{1}}字母,这对我来说毫无意义。

如何简单:

name, password, age

哦,“你好”是一个选择不当的名字(我们不能推断它包含的内容)。哦,“x”,“y”和“长度”也被严格选择,(长度是什么?)。只需为变量选择一个好名称,你的bug就会变得很明显。

答案 1 :(得分:0)

你为什么不写:

function allowDrop(ev) {
  ev.preventDefault();
}
function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}

with open('db.csv', 'a') as testfile: csv_writer = csv.writer(testfile) csv_writer.writerow(hello) 已经是一个列表,不需要你使用的构造。