Python Forum

Full Version: split file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a file with many rows.
This is one of the lines:

2015-03-05 22:08:06.636451 2321

so in order to separate it, I wrote the following:
from astral import Astral 
from scipy import *
from pylab import*
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import datetime
from datetime import timezone
from datetime import timedelta
import matplotlib.dates as dates
import pandas as pd  
import pytz

file=open('bird_jan25jan16.txt','r')

#Turning datas in file into lists
orig_date=[]
orig_time=[]
movements=[]

for i in file:
    tempsplit=i.split(' ')
    orig_date.append(tempsplit[0])
    orig_time.append(tempsplit[1])
    movements.append(tempsplit[2])
but when I print 'movements',
I get

Output:
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''
I am assuming that the splitting is wrong, but not sure.
How do I correct it?
I think everything is answered here: https://python-forum.io/Thread-utc-to-ce...8#pid81208
You have a blank line with many whitespaces. Use split without arguments. If your data is corrupt (blank lines/missing columns/too much), then put a use the split function in a try block, except ValueError and continue if this happens. Then this data is left out. When the format is wrong, it may end into a empty result Wall