0

I am working with a .txt file. This has 100 rows and 5 columns. I need to divide it in five vectors of lenght 100, one for each column. I am trying to follow this: Reading specific columns from a text file in python.

However, when I implement it as:

token = open('token_data.txt','r')
linestoken=token.readlines()
resulttoken=[]
for x in linestoken:
    resulttoken.append(x.split(' ')[1])
token.close()

I don't know how this is stored. If I write print('resulttoken'), nothing appears on my screen.

Can someone please tell me what I am doing wrong?

Thanks. part of my text file

Blub Bla
  • 136
  • 8
slow_learner
  • 337
  • 1
  • 2
  • 15
  • Why do you have ' at last print? It will only print text resulttoken – Matej Feb 17 '19 at 11:32
  • 1
    Shouldn't it be `print(resulttoken)`, without the inverted commas (' ')? – amanb Feb 17 '19 at 11:40
  • Please show your "text file" as actual text, not as a graphic. There are some details, such as the separator character(s), that are not clear in the graphic. – Rory Daulton Feb 17 '19 at 11:53

4 Answers4

4

x.split(' ') is not useful, because columns of your text file separated by more than one space. Use x.split() to ignore spaces:

token = open('token_data.txt','r')
linestoken=token.readlines()
tokens_column_number = 1
resulttoken=[]
for x in linestoken:
    resulttoken.append(x.split()[tokens_column_number])
token.close()
print(resulttoken)
1

Well, the file looks like to be split by table rather than space, so try this:

token = open('token_data.txt','r')
linestoken=token.readlines()
tokens_column_number = 1 resulttoken=[] for x in linestoken:
    resulttoken.append(x.split('\t'))
token.close()
print(resulttoken)
JOHNKYON
  • 83
  • 1
  • 7
0

You want a list of five distinct lists, and append to each in turn.

columns = [[]] * 5
with open('token_data.txt','r') as token:
    for line in token:
        for field, value in enumerate(line.split()):
             columns[field].append(value)

Now, you will find the first value from the first line in columns[0][0], the second value from the first line in columns[1][0], the first value from the second line in columns[0][1], etc.

To print the value of a variable, don't put quotes around it. Quotes create a literal string.

print(columns[0][0])

prints the value of columns[0][0] whereas

print('columns[0][0]')

simply prints the literal text "columns[0][0]".

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can use data_py package to read column wise data in FORTRAN style. Install this package using

pip install data-py

Usage Example

from data_py import datafile
NoOfLines=0   
lineNumber=2  # Line number to read (Excluding lines starting with '#')
df1=datafile("C:/Folder/SubFolder/data-file-name.txt")
df1.separator=","  # No need to specify if separator is space(" ") and for 'tab' separated values use '\t'
NoOfLines=df1.lines  # Total number of lines in the data file (Excluding lines starting with '#')
[Col1,Col2,Col3,Col4,Col5]=["","","","",""]  # Initial values
[Col1,Col2,Col3,Col4,Col5]=df1.read([Col1,Col2,Col3,Col4,Col5)],lineNumber)
print(Col1,Col2,Col3,Col4,Col5)  # In str format

For details please follow the link https://www.respt.in/p/python-package-datapy.html

ddeb
  • 1