Reading python text file

I have a text file from which I need each column, preferably in a dictionary or list, format:

N ID REMAIN VERS 2 2343333 bana twelve 3 3549287 moredp twelve 3 9383737 hinsila twelve 3 8272655 hinsila eight 

I tried:

 crs = open("file.txt", "r") for columns in ( raw.strip().split() for raw in crs ): print columns[0] 

Result = "Indexing Error"

Also tried:

 crs = csv.reader(open(file.txt", "r"), delimiter=',', quotechar='|', skipinitialspace=True) for row in crs: for columns in row: print columns[3] 

It seems like every char reads like a column instead of every word "

I would like to get four columns, i.e.:

 2 2343333 bana twelve 

into separate dictionaries or lists

Any help is great, thanks!

+6
source share
6 answers

This works fine for me:

 >>> crs = open("file.txt", "r") >>> for columns in ( raw.strip().split() for raw in crs ): ... print columns[0] ... N 2 3 3 3 

If you want to convert columns to rows, use zip .

 >>> crs = open("file.txt", "r") >>> rows = (row.strip().split() for row in crs) >>> zip(*rows) [('N', '2', '3', '3', '3'), ('ID', '2343333', '3549287', '9383737', '8272655'), ('REMAIN', 'bana', 'moredp', 'hinsila', 'hinsila'), ('VERS', 'twelve', 'twelve', 'twelve', 'eight')] 

If you have blank lines, filter them before using zip.

 >>> crs = open("file.txt", "r") >>> rows = (row.strip().split() for row in crs) >>> zip(*(row for row in rows if row)) [('N', '2', '3', '3', '3'), ('ID', '2343333', '3549287', '9383737', '8272655'), ('REMAIN', 'bana', 'moredp', 'hinsila', 'hinsila'), ('VERS', 'twelve', 'twelve', 'twelve', 'eight')] 
+8
source
 >>> with open("file.txt") as f: ... c = csv.reader(f, delimiter=' ', skipinitialspace=True) ... for line in c: ... print(line) ... ['N', 'ID', 'REMAIN', 'VERS', ''] #that '' is for leading space after columns. ['2', '2343333', 'bana', 'twelve', ''] ['3', '3549287', 'moredp', 'twelve', ''] ['3', '9383737', 'hinsila', 'twelve', ''] ['3', '8272655', 'hinsila', 'eight', ''] 

Or, the old-fashioned way:

 >>> with open("file.txt") as f: ... [line.split() for line in f] ... [['N', 'ID', 'REMAIN', 'VERS'], ['2', '2343333', 'bana', 'twelve'], ['3', '3549287', 'moredp', 'twelve'], ['3', '9383737', 'hinsila', 'twelve'], ['3', '8272655', 'hinsila', 'eight']] 

And to get the column values:

 >>> l [['N', 'ID', 'REMAIN', 'VERS'], ['2', '2343333', 'bana', 'twelve'], ['3', '3549287', 'moredp', 'twelve'], ['3', '9383737', 'hinsila', 'twelve'], ['3', '8272655', 'hinsila', 'eight']] >>> {l[0][i]: [line[i] for line in l[1:]] for i in range(len(l[0]))} {'ID': ['2343333', '3549287', '9383737', '8272655'], 'N': ['2', '3', '3', '3'], 'REMAIN': ['bana', 'moredp', 'hinsila', 'hinsila'], 'VERS': ['twelve', 'twelve', 'twelve', 'eight']} 
+6
source
 with open("path\sample1.csv") as f: for line in f: print line 

// read line by line line by line

+1
source

You can use list comprehension as follows:

 with open("split.txt","r") as splitfile: for columns in [line.split() for line in splitfile]: print(columns) 

Then you will get it in a 2d array, allowing you to group it the way you like.

0
source

just use a list of lists

 import csv columns = [[] for _ in range(4)] # 4 columns expected with open('path', rb) as f: reader = csv.reader(f, delimiter=' ') for row in reader: for i, col in enumerate(row): columns[i].append(col) 

or if the number of columns should increase dynamically:

 import csv columns = [] with open('path', rb) as f: reader = csv.reader(f, delimiter=' ') for row in reader: while len(row) > len(columns): columns.append([]) for i, col in enumerate(row): columns[i].append(col) 

In the end, you can print your columns with:

 for i, col in enumerate(columns, 1): print 'List{}: {{{}}}'.format(i, ','.join(col)) 
0
source

How about this?

 f = open("file.txt") for i in f: k = i.split() for j in k: print j 
0
source

Source: https://habr.com/ru/post/897680/


All Articles