Reputation: 334
I have obtained a dataset in text format and want to perform analysis on that using Python. The data looks like this:
1 1 -0.0007 -0.0004 100.0 518.67 641.82 1589.70 1400.60 14.62 21.61 554.36 2388.06 9046.19 1.30 47.47 521.66 2388.02 8138.62 8.4195 0.03 392 2388 100.00 39.06 23.4190
1 2 0.0019 -0.0003 100.0 518.67 642.15 1591.82 1403.14 14.62 21.61 553.75 2388.04 9044.07 1.30 47.49 522.28 2388.07 8131.49 8.4318 0.03 392
I want to read this data like a csv file in Python with proper column names(I will define that). Any ideas on how to do that would be appreciated.
Upvotes: 0
Views: 165
Reputation: 591
Suppose the text file is temp.txt
and the separator between your columns is space, you can then use read_csv
function to read such file:
import pandas as pd
names = ['col1', 'col2', 'col3'] # define your column names here, the lentgh of the list should match the actual number of columns you load into the dataframe
df = pd.read_csv('temp.txt', delimiter=' ', header=None, names=names) # use header=None to indicate that your file has no header
Upvotes: 1
Reputation: 844
Try read_csv from pandas (documentation) and define your delimiter with space, like this:
data = pandas.read_csv(delimiter: ' ')
Upvotes: 1
Reputation: 731
Read and use split(' ') to split it into columns. Maybe assign it to a dataframe for easier access.
file1 = open("myfile.txt","r+")
print ("Output of Read function is")
lines = file1.readlines()
print("Col1 Col2 ....")
for line in lines:
temp = line.split(' ')
print(temp[0],temp[1])
Upvotes: 1
Reputation: 457
You could try to read the file line by line, then split each line by spaces and have each element in the split list be associated to a certain column name.
You can use something like that to start:
with open('filename') as f:
lines = f.readlines()
for line in lines:
l = line.split(" ");
for el in l:
#do stuff
Upvotes: 2