Python 从文件中读取数据并将数据放入数据库的元组中

Python 从文件中读取数据并将数据放入数据库的元组中,python,database,tuples,Python,Database,Tuples,所以我需要将员工的信息输入到不同的字符串或文件的元组中。格式是已知的,因此我可以提取它们,并知道需要使用变量的顺序,但我需要将每个字符串拆分为5个不同的项,并将其转换为元组格式或dict employeeList = list() employeeDict = {} f = open("/Users/JamesDonnelly/Downloads/emps.txt") myLine = f.readline() while (len(myLine)>0): for word in

所以我需要将员工的信息输入到不同的字符串或文件的元组中。格式是已知的,因此我可以提取它们,并知道需要使用变量的顺序,但我需要将每个字符串拆分为5个不同的项,并将其转换为元组格式或dict

employeeList = list()
employeeDict = {}

f = open("/Users/JamesDonnelly/Downloads/emps.txt")
myLine = f.readline()
while (len(myLine)>0):
    for word in myLine.split(' '):
        print(word)
        if len(str(word)) != 0 :
            variable = word
    myLine = f.readline()
f.close()

现在,这只是打印出文件中5个单词中的每一个。首先是客户ID,然后是工资、工作等。一共有5个。我需要分配前5到5个不同的变量,然后循环并分配给下5个。然后,在它是一个很好的列表或dict或database-y设置列表之后,我可以循环搜索特定的内容并返回它们。

您可以这样分配它们:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
然后,您将每个信息都包含在一个变量中,现在可以将它们分配给具有关键字customerID的字典:

首先创建字典日志={},然后:


您可以这样分配它们:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
然后,您将每个信息都包含在一个变量中,现在可以将它们分配给具有关键字customerID的字典:

首先创建字典日志={},然后:


又短又干净。您可以用这种简单的方法从列表/元组中提取变量

with open("/Users/JamesDonnelly/Downloads/emps.txt") as f:
    for line in f:
        customerID, pay, job, foo, foo2 = line.strip().split()

又短又干净。您可以用这种简单的方法从列表/元组中提取变量

with open("/Users/JamesDonnelly/Downloads/emps.txt") as f:
    for line in f:
        customerID, pay, job, foo, foo2 = line.strip().split()
我会写:

customer=['Cust ID', 'Pay', 'Job', 'X', 'Y']
data={field:[] for field in customer}
with open(in_file) as fin:
    for line in fin:
        for field, datum in zip(customer,line.split()):
             data[field].append(datum)
然后,给出如下文件:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
您将得到如下数据结构:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
然后通过以下方式访问数据:

>>> data['Job'][1]
'8'
我会写:

customer=['Cust ID', 'Pay', 'Job', 'X', 'Y']
data={field:[] for field in customer}
with open(in_file) as fin:
    for line in fin:
        for field, datum in zip(customer,line.split()):
             data[field].append(datum)
然后,给出如下文件:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
您将得到如下数据结构:

customerID, pay, job, something1, something2 = myLine.split(' ')
1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15
{'Y': ['5', '10', '15'], 'Pay': ['2', '7', '12'], 'Job': ['3', '8', '13'], 'Cust ID': ['1', '6', '11'], 'X': ['4', '9', '14']}
然后通过以下方式访问数据:

>>> data['Job'][1]
'8'

你能给我们举一个emps.txt的一些行的例子,以及你想如何组织员工吗?为什么不简单:a,b,c,d,e=myLine.split:?你能给我们举一个emps.txt的一些行的例子,以及你想如何组织员工吗?为什么不简单:a,b,c,d,e=myLine.split:?你想怎么组织,提取完元组后,将其放入列表或类似格式,以便搜索和显示结果?创建一个列表并使用“附加”将值放入列表的末尾。如何,提取完元组后,将其放入列表或类似格式,以便搜索和显示结果?创建一个列表并使用“附加”将值放入列表末尾。