Python Forum
Sorting columns from text file help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting columns from text file help
#1
Hello! I'm very new to Python and need help figuring out how to sort by columns from a text file which is being throw off by the Date column (which has single white spaces, so trying to separate by tabs is not working).

Text file (sample) contents is below (actual text file is several thousand lines long).

Date           	   		Num1    Num2   
Sat. Aug 10, 2019              41          59          
Wed. Aug 07, 2019              47          8              
Sat. Aug 03, 2019               45          68  



The CODE I tried:

df = pd.read_csv('columns_test.txt', names=['Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df
The OUTPUT I get is:

Date	Num1	Num2
Sat.	Aug	10,	2019	41	59
Wed.	Aug	07,	2019	47	8
Sat.	Aug	03,	2019	45	68
I have also tried other parameters such as sep='\t' , delim_whitespace=True , etc. But it's the Date column with the single white spaces between that is throwing off the columns being separated under the right heading. My goal is to import the data from the text file for data analysis but they need to be sorted under the right columns.

Any advice on how I can achieve this would be greatly appreciated!
Reply
#2
(Nov-15-2019, 05:10 PM)nickglobal101 Wrote: The CODE I tried:

df = pd.read_csv('columns_test.txt', names=['Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df

Hi!

Have you tried this?:

df = pd.read_csv('columns_test.txt', names=['Weekday','Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 519 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Create csv file with 4 columns for process mining thomaskissas33 3 693 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,168 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,064 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  deleting columns in CSV file astral_travel 8 2,165 Nov-26-2022, 09:36 PM
Last Post: astral_travel
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 752 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,842 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  rows from sql query need to write to a file as columns sjcsvatt 6 2,333 Oct-09-2021, 12:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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