0

i need to import a file into an array. The file is like this

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

. and i need to import it into a list like this

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'
]

Now, i don't know what am doing wrong, i used the code

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

but am not getting that, i tried to export it using

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

but i couldn't get the list am expacting. in the command line, if i print out, i get soemthing close,but i can't export that

3 Answers 3

2

You should use readlines() instead of readline() - notice ending s:

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

Also, when saving/writing out, do this:

joinliens = ''.join(times)
with open('ext.txt', 'w') as f:
    f.write(joinliens)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, i forgot to put 's' i mean readlines, and i kept on getting error, but the saving part is giving me a weird result. i don't know why. its spilting the result
@idrisbadmus What do you mean it is "splitting the result"? If it adds an extra empty line - that's because I wrote '\n'.join(times) and it should have been like how you did originally because readlines() keeps the ending newline character.
1

You might want to try 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

Comments

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

for line in impo.readline(): didnt work, because that read one line and looped over each character in the line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.