Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
split file
#1
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?
Reply
#2
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,399 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 2,096 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  How to split file by same values from column from imported CSV file? Paqqno 5 2,799 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,580 Feb-11-2022, 03:18 PM
Last Post: snippsat
  split txt file data on the first column value shantanu97 2 2,444 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  [split] Help- converting file with pyton script eltomassito 6 3,257 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Split Characters As Lines in File quest_ 3 2,518 Dec-28-2020, 09:31 AM
Last Post: quest_
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,358 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Split and sort input file aawaleh 4 2,998 Apr-10-2020, 09:59 PM
Last Post: aawaleh
  Split csv file based on column value soli004 4 6,123 Oct-22-2019, 05:53 AM
Last Post: soli004

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020