the phoenix
the phoenix

Reputation: 739

How to import this file in pandas?

I am trying to import the data from a ".txt" file using read_csv but I did not manage to import it correctly. All the columns are being imported as one. I want to have four separate columns. This is a sample from the file:

    TIME                 A        B       C
----                ------- ------- -------
1599004800003299944 51      -731    17271
1599004800008323314 47      -606    17245
1599004800013323079 71      -755    17300
1599004800018324785 23      -621    17273
1599004800023317477 42      -705    17268
1599004800028280442 48      -715    17239
 

Upvotes: 0

Views: 50

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 148910

You must first describe your file in natural language:

  1. fields are separated with a variable number of white spaces
  2. the first row contains the headers
  3. the second row should be ignored

Now you have just to read the relevant doc and translate it into the appropriate parameters:

  1. sep=r'\s+' or delim_whitespace=True (the latter is less commonly used AFAIK)
  2. header=0
  3. skiprows=[1]

It gives:

df = pd.read_csv(file_name, sep=r'\s+', header=0, skiprows=[1])

Upvotes: 2

Daweo
Daweo

Reputation: 36450

Use \s+ (1 or more whitespaces) as sep:

import pandas as pd
df = pd.read_csv('file.txt', sep='\s+')

Upvotes: 1

Anton Khanaev
Anton Khanaev

Reputation: 11

Try out this code:

data = pd.read_csv('data.txt', sep=' ', skiprows=2)

sep is for delimeter between columns

skiprows leaves first 2 lines unread

Upvotes: 0

Related Questions