在CSV文件中创建列

时间:2017-10-23 19:04:44

标签: python python-3.x csv

我在代码上苦苦挣扎,我不能使用Pandas。我看了其他解释,但我认为他们很难理解。

file=open("Details.csv","a" )
file.append(str"Username" + str"Password" + str"Age" + str"Topic" + 
str"Difficulty" + str"Score"\n)
print("Before we start, we need to register an account.\n")           
User=input(str("Enter your name:\n "))
Age=input("Enter your age:\n")
print("Great! Your username is set to: %s."%(User[0]+User[1]+User[2]+ Age))
Pass=input("Enter a password for your account:\n ")

所以我试图在csv文件中获得6列,用户名,密码,年龄,主题,难度和分数。

提前致谢。

2 个答案:

答案 0 :(得分:2)

修改

我经历了所有麻烦只是为了不真正回答问题标题的范围。如果您使用csv模块,则写入.csv非常简单。模块将文件写为iterables(即值列表)。它通过调用csv.writer.writerow()来实现。没有任何约束条件,预期标头的长度必须等于您写入文件的任何可迭代的长度。

<强>响应:

我总是使用标准库的忠实粉丝。该库内部是一个名为csv的模块。您尝试做的所有事情(或者至少是您告诉我们的事情)都可以使用此模块完成。

让我们来解决问题。

首先,我们将看看您尝试过的内容:

# you've opened a file for appending (the mode = 'a')
# why do we need to append it? are we assuming that there is already data?
file=open("Details.csv","a" )

# this shouldn't ever work (I dont think ive ever seen this either)
# are these supposed to be headers?
file.append(str"Username" + str"Password" + str"Age" + str"Topic" + 
str"Difficulty" + str"Score"\n)
print("Before we start, we need to register an account.\n")

# okay you are getting a user input
# however, the input() method returns 'str'
# so you dont need to call str() on a 'str'!           
User=input(str("Enter your name:\n "))

# now here if you want an 'int' for the age you call int()
Age=input("Enter your age:\n")
print("Great! Your username is set to: %s."%(User[0]+User[1]+User[2]+ Age))

# why dont you call str() here like you did before?
# is this a different type?
Pass=input("Enter a password for your account:\n ")

# woah, you never do anything with your inputs
# nor do you close the file!

显然这里有很多未知数。因此,让我们来看看你应该做什么:

import csv

myCSV = '/path/to/Details.csv'
headers = ['Username', 'Password', 'Age', 'Topic', 'Difficulty', 'Score']

# using 'with open()' is called context managing
# with is the context manager
# what this means is when we are done with the file it auto-closes
# I am just going to assume nothing is in the file
# if there is, next time just dont call w.writerow(headers)
with open(myCSV, 'w') as f:
    w = csv.writer(f, delimiter=',')
    w.writerow(headers)

    # we can drop the '\n' because after print() we go to the next line always
    print("Before we start, we need to register an account.")
    user = input("Enter your name:\n")

    # since you concatenate below, I'll assume you want a 'str'
    age = input("Enter your age:\n")
    print ("Great! Your username is set to: {}".format(user[:3] + age))

    pass = input("Enter a password for your account:\n")

    # then do the same for Topic, Difficulty and Score

    values = [user, pass, age, topic, diff, score]
    w.writerow(values)

对于字符串格式化方法,请查看this以获取更新语法。

答案 1 :(得分:0)

我建议您查看csv库(https://docs.python.org/2/library/csv.html)并按此顺序执行操作

with open('Details.csv', 'wb') as file:
    writer = csv.writer(file)
    writer.writerow(["Username", "Password", "Topic", "Difficulty", "Score"])

这将创建包含您指定的标题名称的列。