来自TXT的Numpy 2D数组

时间:2015-10-07 07:11:19

标签: python arrays numpy

你好我需要从存储在txt文件中的数据创建numpy二维数组。我想要实现的主要目标是使用名称而不是索引[0] [1]等。我希望能够像这样调用它:myarray [' Date'] [' 0& #39;]例如.TXT的结构是:2015-12-01,Jane,Adam,3.0,5.7,2.4 这是我的代码

from pylab import *
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY
import numpy as np

mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays    = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12

#Here i create a class to store the array
class my_data:
    datalist=np.empty([],dtype={'names': ('Date', 'Name', 'Name2', 'Start', 'End','Avg'),
                    'formats': ('S10','S10', np.float, np.float, np.float,np.float)})

work=my_data()

#Here my read data function
def DataLd():
   list=[]
   data=[]
   NDate=0
   with open('DataBase/table.txt', "r") as f:
    for line in f:
        data=line.split(',')
        NDate = date2num(datetime.strptime(data[0], "%Y-%M-%d"))
        work.datalist=np.append(work.datalist,np.array([data[0],data[1],data[2],data[3],data[4],data[5]]),axis=0)
    print(work.datalist)

DataLd()

这就是错误

  

ValueError:无法连接零维数组

1 个答案:

答案 0 :(得分:0)

我建议以这种方式将import concurrent.futures import urllib.request URLS = ['http://www.foxnews.com/', 'http://www.cnn.com/', 'http://europe.wsj.com/', 'http://www.bbc.co.uk/', 'http://some-made-up-domain.com/'] # Retrieve a single page and report the url and contents def load_url(url, timeout): with urllib.request.urlopen(url, timeout=timeout) as conn: return conn.read() # We can use a with statement to ensure threads are cleaned up promptly with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: # Start the load operations and mark each future with its URL future_to_url = {executor.submit(load_url, url, 60): url for url in URLS} for future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] try: data = future.result() except Exception as exc: print('%r generated an exception: %s' % (url, exc)) else: print('%r page is %d bytes' % (url, len(data))) 加载为DataFrame而不是txt,这样您就可以按名称获取单元格:

numpy.ndarray