Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 选择一些字段_Python_Database_Python 3.x - Fatal编程技术网

Python 选择一些字段

Python 选择一些字段,python,database,python-3.x,Python,Database,Python 3.x,我使用Python3.3和格式为txt的数据库 数据库包含6列 id, date, r1, numbers, r2, numbers 像这样: id date r1 numbers r2 numbers 1 25/03/2013 Ba 11 12 13 14 15 Da 33 44 55 66 77 2 26/03/2013 Ba 12 32 33 55 66 Da 22 11 14 17 23 3 27/03/2013 Ba

我使用Python3.3和格式为txt的数据库

数据库包含6列

id, date, r1, numbers, r2, numbers
像这样:

id  date        r1  numbers         r2  numbers
1   25/03/2013  Ba  11 12 13 14 15  Da  33 44 55 66 77
2   26/03/2013  Ba  12 32 33 55 66  Da  22 11 14 17 23
3   27/03/2013  Ba  32 33 34 35 36  Da  37 38 39 40 41
我用Python实现的代码是:

f = open('db.txt','r')

for line in f.read().split('\n'):

    print (line)
f.close()
我只想打印第四列,包括标题

有可能吗?

解决方案已从原始海报的注释中删除

解决办法是:

for line in open("db.txt"):
    columns = line.split("\t")
    print (columns[3]) # indexing starts at zero

当然是。您是否费心定义“第四列”了?列选项卡是分隔的还是多个空格?安装第三方库是否存在问题?我使用tab作为分隔符,而不安装第三方库。这是解决方案:对于打开的行(“dbx.txt”):columns=line.split(“\t”)print(columns[3])#索引从零开始