Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 打印包含日期、时间和值数据的txt文件_Python_Date_Time_Load - Fatal编程技术网

Python 打印包含日期、时间和值数据的txt文件

Python 打印包含日期、时间和值数据的txt文件,python,date,time,load,Python,Date,Time,Load,我对Python编程相当陌生,已经遇到了一个让我发疯的问题。我一直在寻找这个问题,甚至在堆栈溢出的时候。然而,我没有找到任何解决问题的方法,这让我注册了这个网站 然而,这是我的问题: 我有几个txt文件,包含3列。第一列可以忽略,第二列包含日期和时间的混合物,用字母“T”分隔,第三列包含值(压力、温度等等) 现在,我想做的是,在x轴上绘制第二列(时间和日期),在y轴上绘制值 我已经尝试了很多代码——也有一些在stack overflow中描述过——但没有一个是我正在搜索的代码,并且没有得到正确的

我对Python编程相当陌生,已经遇到了一个让我发疯的问题。我一直在寻找这个问题,甚至在堆栈溢出的时候。然而,我没有找到任何解决问题的方法,这让我注册了这个网站

然而,这是我的问题:

我有几个txt文件,包含3列。第一列可以忽略,第二列包含日期和时间的混合物,用字母“T”分隔,第三列包含值(压力、温度等等)

现在,我想做的是,在x轴上绘制第二列(时间和日期),在y轴上绘制值

我已经尝试了很多代码——也有一些在stack overflow中描述过——但没有一个是我正在搜索的代码,并且没有得到正确的结果

更详细地说,这是my txt文件的外观:

#MagPy ASCII
234536326.4562014-06-17T14:23:00.000000459.74633940044 674346235.2352014-06-17T14:28:00.000000462.8783040474751 等等

忘掉第一列吧。只有第二个和第三个是相关的。所以在这里,我想,我必须跳过第一行(和第一列),对吗

然而,我无法解决的是,在第二列中有了这个“T”,它就变成了字符串格式。 我遇到的许多错误之一是:无法将字符串转换为浮点

我搜索了stack overflow,发现了以下代码:

 x, y = np.loadtxt('example.txt', dtype=int, delimiter=',',
              unpack=True, usecols=(1,2))

 plt.plot(x, y)

 plt.title('example 1')
 plt.xlabel('D')
 plt.ylabel('Frequency')
 plt.show()
我将“usecols”编辑为1和2,但是使用这段代码,我得到了错误:列表索引超出范围

所以,不管我做什么,我随时都会出错。我唯一想要的是一个绘图(带有matplotlib),它在x轴上包含时间和日期,在y轴上包含值(例如上面的459.74633940044)。 说到我需要的:最后,我必须在一个图中放上几个图表(大约4-6),它们是由许多txt文件数据生成的


拜托,有人能帮我吗?我非常感谢你的帮助

这是numpy datetime格式。您需要为datetime字段添加显式转换器。包含其他格式详细信息。问题显示如何填写转换器参数

date, closep, highp, lowp, openp, volume = 
    np.loadtxt(f, delimiter=',', unpack=True, 
        converters={0:mdates.strpdate2num('%d-%b-%y')})

这足以让您找到完整的解决方案吗?

这是numpy datetime格式。您需要为datetime字段添加显式转换器。包含其他格式详细信息。问题显示如何填写转换器参数

date, closep, highp, lowp, openp, volume = 
    np.loadtxt(f, delimiter=',', unpack=True, 
        converters={0:mdates.strpdate2num('%d-%b-%y')})

这足以让您找到完整的解决方案吗?

首先,感谢您的回复!不幸的是,这根本不起作用。我用你的转换器试过了,并把它和我的代码结合起来,但效果不好。然后我尝试了以下代码:

# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d %m %Y %H %M %S'))

# Read data from 'file.dat'
dates, levels = np.genfromtxt('BMP085_10085001_0001_201508.txt',    # Data to be read
                          delimiter=19,  # First column is 19 characters wide
                          converters={1: datefunc}, # Formatting of column 0
                          dtype=float,   # All values are floats
                          unpack=True)   # Unpack to several variables

fig = plt.figure()
ax = fig.add_subplot(111)

# Configure x-ticks
ax.set_xticks(dates) # Tickmark + label at every plotted point
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))

ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('title')
ax.set_ylabel('Waterlevel (m)')
ax.grid(True)

# Format the x-axis for dates (label formatting, rotation)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()

fig.show()

我又得到一个列表索引超出范围的错误。说真的,我不知道有什么可能的解决方案可以让它看起来像这样:{}-请参阅页面底部的图表

首先,感谢您的回复!不幸的是,这根本不起作用。我用你的转换器试过了,并把它和我的代码结合起来,但效果不好。然后我尝试了以下代码:

# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d %m %Y %H %M %S'))

# Read data from 'file.dat'
dates, levels = np.genfromtxt('BMP085_10085001_0001_201508.txt',    # Data to be read
                          delimiter=19,  # First column is 19 characters wide
                          converters={1: datefunc}, # Formatting of column 0
                          dtype=float,   # All values are floats
                          unpack=True)   # Unpack to several variables

fig = plt.figure()
ax = fig.add_subplot(111)

# Configure x-ticks
ax.set_xticks(dates) # Tickmark + label at every plotted point
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))

ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('title')
ax.set_ylabel('Waterlevel (m)')
ax.grid(True)

# Format the x-axis for dates (label formatting, rotation)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()

fig.show()

我又得到一个列表索引超出范围的错误。说真的,我不知道有什么可能的解决方案可以让它看起来像这样:{}-请参阅页面底部的图表

我成功地做到了以下几点。首先,可以将ISO8601日期作为字符串读取(因此,基本读取一行并用逗号拆分即可)。要将日期字符串转换为datetime对象,可以使用

import dateutil   

# code to read in date_strings and my_values as lists goes here ...

# Here's the magic to parse the ISO8601 strings and make them into datetime objects

my_dates = dateutil.parser.parse(date_strings)

# Now plot the results

fig = plt.figure()
ax = fig.add_subplot(1,1,1)    
ax.plot_date(x=my_dates, y=my_values, marker='o', linestyle='')
ax.set_xlabel('Time')

我成功地做到了以下几点。首先,可以将ISO8601日期作为字符串读取(因此,基本读取一行并用逗号拆分即可)。要将日期字符串转换为datetime对象,可以使用

import dateutil   

# code to read in date_strings and my_values as lists goes here ...

# Here's the magic to parse the ISO8601 strings and make them into datetime objects

my_dates = dateutil.parser.parse(date_strings)

# Now plot the results

fig = plt.figure()
ax = fig.add_subplot(1,1,1)    
ax.plot_date(x=my_dates, y=my_values, marker='o', linestyle='')
ax.set_xlabel('Time')

好啊不知怎的,我的答案比你的答案高!?没问题。他们通常是按最多的选票排序的;我不知道关系是如何解决的。您可能可以在元组中查找此信息,将来的讨论应该在元组中进行。不知怎的,我的答案比你的答案高!?没问题。他们通常是按最多的选票排序的;我不知道关系是如何解决的。您可能可以在元组中查找这一点——将来的讨论应该在那里进行。