简体   繁体   中英

Reading a txt file and saving individual columns as lists

I am trying to read a .txt file and save the data in each column as a list. each column in the file contains a variable which I will later on use to plot a graph. I have tried looking up the best method to do this and most answers recommend opening the file, reading it, and then either splitting or saving the columns as a list. The data in the .txt is as follows -

0   1.644231726
0.00025 1.651333945
0.0005  1.669593478
0.00075 1.695214575
0.001   1.725409504

the delimiter is a space '' or a tab '\\t' . I have used the following code to try and append the columns to my variables -

import csv

with open('./rvt.txt') as file:
    readfile = csv.reader(file, delimiter='\t')
    time = []
    rim = []
    for line in readfile:
        t = line[0]
        r = line[1]
        time.append(t)
        rim.append(r)

print(time, rim)

However, when I try to print the lists, time and rim, using print(time, rim) , I get the following error message -

r = line[1]
IndexError: list index out of range

I am, however, able to print only the 'time' if I comment out the r=line[1] and rim.append(r) parts. How do I approach this problem? Thank you in advance!

I would suggest the following:

import pandas as pd

df=pd.read_csv('./rvt.txt', sep='\t'), header=[a list with your column names])

Then you can use list(your_column) to work with your columns as lists

The problem is with the delimiter. The dataset contain multiple space ' '.

When you use '\\t' and print line you can see it's not separating the line with the delimiter.

eg:

['0   1.644231726']
['0.00025 1.651333945']
['0.0005  1.669593478']
['0.00075 1.695214575']
['0.001   1.725409504']

To get the desired result you can use (space) as delimiter and filter the empty values:

readfile = csv.reader(file, delimiter=" ")
time, rim = [], []
for line in readfile:
    line = list(filter(lambda x: len(x), line))
    t = line[0]
    r = line[1]

Here is the code to do this:

import csv

with open('./rvt.txt') as file:
    readfile = csv.reader(file, delimiter=” ”)
    time = []
    rim = []
    for line in readfile:
        t = line[0]
        r = line[1]
        time.append(t)
        rim.append(r)
print(time, rim)

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