Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python在excel的新列中输入数据_Python_Excel_Python 3.x_Python 3.5_Xls - Fatal编程技术网

使用Python在excel的新列中输入数据

使用Python在excel的新列中输入数据,python,excel,python-3.x,python-3.5,xls,Python,Excel,Python 3.x,Python 3.5,Xls,我正在使用Python 3.5.2 这是我的意见: print("") print("-----------------------------------------------------------------------------------------------------------------------------------------------------------") print("| (1) Enter new student details

我正在使用Python 3.5.2

这是我的意见:

print("")
print("-----------------------------------------------------------------------------------------------------------------------------------------------------------")
print("|      (1) Enter new student details        |              (2) Edit existing student details           |           (3) Retrieve Student details            |")
print("-----------------------------------------------------------------------------------------------------------------------------------------------------------")
print("")
option=int(input("Select an option from the menu, 1/2/3/: "))
if option==1:
    f=open("Student Details.xls" , "w")
    f.write("Forename"+"\n")
    fname=input("Enter the student's first name: ")
    f.write(fname)
    f.write("Surname"+"\n")
    sname=input("Enter the student's surname: ")
    f.write(sname)
    f.write("DOB"+"\n")
    dob=input("Enter the student's DOB: ")
    f.write(dob)
    f.write("Postcode"+"\n")
    pcode=input("Enter the student's postcode: ")
    f.write(pcode)
运行此操作时,它将创建一个excel文件并写入其中。但是,它将所有细节都写在一个单元格中。我希望excel文件如下所示:

Forename  Surname   DOB        Postcode
Jim       Bob       12/12/12   w7 1eu
我研究了如何在不同的列中输入数据,但是我找到的所有解决方案都使用导入xlwt,我无法安装它,因为我没有这台电脑的管理员权限


还有其他方法吗?

我真的建议使用类似的方法,而不是直接处理文件。这使过程简单得多:

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("sample.xlsx")

如果你想把它们放在相邻的列中,添加
\n
可能不是最好的方法…@Coldspeed我该怎么做?@Coldspeed xlrd不是这个版本的python内置的,你可以使用这个库。@Coldspeed你能写一个例子吗?我已经阅读了文档,但代码似乎不起作用。我无法安装模块,因此无法执行“导入工作簿”