我正在尝试将.txt文件中的多列日期和时间数据读入python
文件(具有相同间距)的准确示例如下:
Unrequired info
Unrequired info
Unrequired info
Initial start: Main start: Recovery start: Recovery end: H:
yyyymmdd hh:mm yyyymmdd hh:mm yyyymmdd hh:mm yyyymmdd hh:mm nT
20030817 05:06 20030819 05:06 20030901 05:06 20030902 05:06 -10
20040713 21:22 20040716 23:42 20040717 02:41 20040718 16:09 -93
然后有多行日期时间信息
我想把日期时间信息从单独的列读入单独的列表或数组,可以是字符串,也可以是日期时间格式。我不需要最后一栏的数据
这是我迄今为止尝试的代码:
InitialStart = []
MainStart = []
RecoveryStart = []
RecoveryEnd = []
with open('list.txt', 'r') as file:
lines = file.readlines()[6:]
for row in file:
a, b, c, d, e = row.split()
InitialStart.append(str(a))
MainStart.append(str(b))
RecoveryStart.append(str(c))
RecoveryEnd.append(str(d))
当print(InitialStart)
被添加到代码中时,唯一打印的结果是[]
预期的输出是针对每个元素中日期时间数据作为字符串的列表
一位同事对此给出了答案(^{} ),所以我将回答我自己的问题
listTable = pd.read_csv(filepath_or_buffer='list.txt', sep='\s{2,}', names=['InitialStart', 'MainStart', 'RecoveryStart', 'RecoveryEnd', 'H'], dtype='str', engine='python', skiprows=(0, 1, 2, 3, 4, 5))
sep='\s{2,}'
将分隔设置为空白(\s
),并且{2,}
将其设置为2或更多(可以看到更多命令here)相关问题 更多 >
编程相关推荐