将文件导入数组python

时间:2018-02-11 02:04:26

标签: python extraction

我需要将文件导入数组。该文件是这样的

2017-12-21T14:49:17.518Z
2017-12-21T14:50:49.723Z
2017-12-21T14:50:54.028Z
2017-12-21T14:50:54.343Z
2017-12-21T14:50:59.084Z
2017-12-21T14:50:59.399Z
2017-12-21T14:51:04.142Z
2017-12-21T14:51:04.457Z
2017-12-21T14:51:09.204Z
2017-12-21T14:51:09.521Z
2017-12-21T14:51:14.261Z
2017-12-21T14:51:14.579Z
2017-12-21T14:51:19.326Z
2017-12-21T14:51:19.635Z
2017-12-21T14:51:24.376Z
2017-12-21T14:51:24.691Z
2017-12-21T14:51:29.435Z
2017-12-21T14:51:29.750Z
2017-12-21T14:51:34.498Z
2017-12-21T14:51:34.813Z
2017-12-21T14:51:39.553Z
2017-12-21T14:51:39.868Z
2017-12-21T14:51:44.612Z
2017-12-21T14:51:44.927Z
2017-12-21T14:51:49.675Z
2017-12-21T14:51:49.990Z
2017-12-21T14:51:54.738Z
2017-12-21T14:51:55.042Z

。我需要将它导入到像这样的列表中

times = [
    '2017-12-21T14:49:17.518Z',
    '2017-12-21T14:50:49.723Z',
    '2017-12-21T14:50:54.028Z',
    '2017-12-21T14:50:54.343Z',
    '2017-12-21T14:50:59.084Z',
    '2017-12-21T14:50:59.399Z',
    '2017-12-21T14:51:04.142Z',
    '2017-12-21T14:51:04.457Z',
    '2017-12-21T14:51:09.204Z',
    '2017-12-21T14:51:09.521Z',
    '2017-12-21T14:51:14.261Z',
    '2017-12-21T14:51:14.579Z',
    '2017-12-21T14:51:19.326Z',
    '2017-12-21T14:51:19.635Z',
    '2017-12-21T14:51:24.376Z',
    '2017-12-21T14:51:24.691Z',
    '2017-12-21T14:51:29.435Z',
    '2017-12-21T14:51:29.750Z',
    '2017-12-21T14:51:34.498Z',
    '2017-12-21T14:51:34.813Z'
]

现在,我不知道出了什么问题,我使用了代码

times = []
impo= open('checck.txt','r')
for line in impo.readline():
    times.append(line)

但我没有得到,我试图使用

导出它
joinliens = ''.join(times)
open('ext.txt', 'w').write(joinliens)

但我无法得到列表。在命令行中,如果我打印出来,我会得到关闭,但我无法导出

3 个答案:

答案 0 :(得分:2)

您应该使用readlines()代替readline() - 注意结束 s

for line in impo.readlines():
    times.append(line)

此外,保存/写出时,请执行以下操作:

joinliens = ''.join(times)
with open('ext.txt', 'w') as f:
    f.write(joinliens)

答案 1 :(得分:1)

您可能想尝试pandas

import pandas as pd

df = pd.read_csv('file.csv', header=None, names=['times'])

times = df['times'].tolist()  # output as list

df['times'] = pd.to_datetime(df['times'])  # output as datetime series

答案 2 :(得分:0)

impo = open('checck.txt', 'r')
    times = [line.strip() for line in impo.readlines()]

for line in impo.readline():没有用,因为它读取一条行并循环播放该行中的每个字符