Reputation: 739
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
Reputation: 148910
You must first describe your file in natural language:
Now you have just to read the relevant doc and translate it into the appropriate parameters:
sep=r'\s+'
or delim_whitespace=True
(the latter is less commonly used AFAIK)header=0
skiprows=[1]
It gives:
df = pd.read_csv(file_name, sep=r'\s+', header=0, skiprows=[1])
Upvotes: 2
Reputation: 36450
Use \s+
(1 or more whitespaces) as sep
:
import pandas as pd
df = pd.read_csv('file.txt', sep='\s+')
Upvotes: 1
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