简体   繁体   中英

How to read data from a text file in Python?

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.

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

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])

尝试 read_csv from pandas ( documentation ) 并用空格定义分隔符,如下所示:

data = pandas.read_csv(delimiter: ' ')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM