I have simple txt file with maybe 6 columns and 10000 rows. How can i read only first column and save it in some array?
Every solution that i tried didn't work.
I manage it with octave very simply,
data = load("sample.txt")
first_column = data(:,1)
EDIT: I forgot to metion, I have to rows of strings that i need to skip. My file starts like this:
2000 -89.4010789 736.581586 -825.982664 4988
4000 -86.8499373 707.358765 -794.208703 4952
6000 -87.3911178 680.584591 -767.975709 4968
If you have a CSV
file, then I'd recommend using pandas .
In general though the solution is something like:
DELIMITER = ' '
data = []
with open('test.txt') as fr:
for line in fr:
first_col = line.split('{}'.format(DELIMITER))[0]
data.append(first_col)
You can replace DELIMITER
with whatever you want (spaces, tabs, etc.)
Or, as @cricket_007 says, use the Python csv module. Something like:
lines = csv.reader(open('test.txt', 'rb'), delimiter=DELIMITER)
the following code works if the separator between columns is a tab. Otherwise adjust to whatever the separator is.
import pandas as pd
df = pd.read_csv('sample.txt', header=None, usecols=[1], sep='\t')
note that despite read_csv claiming it reads csv's, it can read any text file with a consistent separator
The built-in csv
module provides this functionality:
import csv
with open('data.dat', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
first_column = [ row[0] for row in reader ]
This code extracts the first column into a list as requested.
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.