简体   繁体   中英

python2.7 define the content of txt file

2330,0.5,13:30:08,121.5,106.5,114.5,115,113.5,114.5,37464,4243,114,1977,113.5,3710,113,5019,112.5,2943,112,1966,114.5,2305,115,4517,115.5,2459,116,2376,116.5,2186,0,1,24,1000,25928305829,name,22,33,44

This is the text file's content. I set the file name in fileLine . It is one line and split by comma.

number,rangep,timep,maxp,minp,openp,highp,lowp,closep,volume,curvolume,openprice,
openbvolume,bp1,bv1,bp2,bv2,bp3,bv3,bp4,bv4,bp5,bv5,sp1,sv1,sp2,sv2,sp3,sv3,sp4,sv4
sp5,sv5,un1,un2,un3,un4,un5,namep,un6,un7,un8 = np.loadtxt(fileLine,delimiter=',',umpack=True)

When I run this it shows all my variables are not defined. How can I set the content to variable, then I can plot the plot.

You cannot split the assignment targets over multiple lines without telling Python explicitly that you are doing so. You could use parenthesis:

(number,rangep,timep,maxp,minp,openp,highp,lowp,closep,volume,curvolume,openprice,
 openbvolume,bp1,bv1,bp2,bv2,bp3,bv3,bp4,bv4,bp5,bv5,sp1,sv1,sp2,sv2,sp3,sv3,sp4,
 sv4,sp5,sv5,un1,un2,un3,un4,un5,namep,un6,un7,un8) = np.loadtxt(fileLine, delimiter=',')

However, I'd not try and assign this many variables from one line, it is rather unwieldy and unreadable.

You do not need to use unpack=True here, you are only loading one line, and you don't need to transpose multiple lines into columns here. In fact, using numpy.loadtext() could well be overkill here as you are unpacking the produced array() object into separate values again.

Given your data line, numpy.txt is the wrong tool ; it expects all values to be of the same type ( float by default) and your data line contains a mix of types.

You should avoid numpy.loadtext() altogether here; you could use:

with open(fileLine) as f:
    row = f.readline().split(',')

and index into that with row[0] , etc.

In this case, you could use the collections.namedtuple() factory to produce a tuple for such a line, allowing you to address individual values by name still, while at the same time avoiding such a monster assignment line:

from collections import namedtuple

Values = namedtuple('Values',
    'number,rangep,timep,maxp,minp,openp,highp,lowp,closep,volume,curvolume,'
    'openprice,openbvolume,bp1,bv1,bp2,bv2,bp3,bv3,bp4,bv4,bp5,bv5,sp1,sv1,sp2,sv2,sp3,'
    'sv3,sp4,sv4,sp5,sv5,un1,un2,un3,un4,un5,namep,un6,un7,un8')

with open(fileLine) as f:
    values = Values(*f.readline().split(','))

Now you can address these as values.number and values.curvolume , etc.

You'll have to convert any values manually though.

Since its a simple file, you can use DictReader from the csv module to get a more manageable data structure:

import csv

fields = ['number','rangep','timep','maxp','minp','openp','highp','lowp','closep','volume','curvolume','openprice','openbvolume','bp1','bv1','bp2','bv2','bp3','bv3','bp4','bv4','bp5','bv5','sp1','sv1','sp2','sv2','sp3','sv3','sp4','sv4','sp5','sv5','un1','un2','un3','un4','un5','namep','un6','un7','un8']

with open('somefile.txt') as f:
   reader = csv.DictReader(f, fieldnames=fields)
   rows = list(reader)

for row in rows:
    print(row['number']) # and so on
f = open(filename, 'r')
line = f.readline()
alist = line.split(',')
f.close()

And then you can just handle the data in the alist. For example: number as alist[0].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM