python怎么导入数据文件,从Python中的文本文件导入数据和变量名

I have a text file containing simulation data (60 columns, 100k rows):

a b c

1 11 111

2 22 222

3 33 333

4 44 444

... where in the first row are variable names, and beneath (in columns) is the corresponding data (float type).

I need to use all these variables with their data in Python for further calculations. For example, when I insert:

print(b)

I need to receive the values from the second column.

I know how to import data:

data=np.genfromtxt("1.txt", unpack=True, skiprows = 1)

Assign variables "manually":

a,b,c=np.genfromtxt("1.txt", unpack=True, skiprows = 1)

But I'm having trouble with getting variable names:

reader = csv.reader(open("1.txt", "rt"))

for row in reader:

list.append(row)

variables=(list[0])

How can I change this code to get all variable names from the first row and assign them to the imported arrays ?

解决方案

Instead of trying to assign names, you might think about using an associative array, which is known in Python as a dict, to store your variables and their values. The code could then look something like this (borrowing liberally from the csv docs):

import csv

with open('1.txt', 'rt') as f:

reader = csv.reader(f, delimiter=' ', skipinitialspace=True)

lineData = list()

cols = next(reader)

print(cols)

for col in cols:

# Create a list in lineData for each column of data.

lineData.append(list())

for line in reader:

for i in xrange(0, len(lineData)):

# Copy the data from the line into the correct columns.

lineData[i].append(line[i])

data = dict()

for i in xrange(0, len(cols)):

# Create each key in the dict with the data in its column.

data[cols[i]] = lineData[i]

print(data)

data then contains each of your variables, which can be accessed via data['varname'].

So, for example, you could do data['a'] to get the list ['1', '2', '3', '4'] given the input provided in your question.

I think trying to create names based on data in your document might be a rather awkward way to do this, compared to the dict-based method shown above. If you really want to do that, though, you might look into reflection in Python (a subject I don't really know anything about).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值