简体   繁体   中英

python reading text file

I have a text file, of which i need each column, preferably into a dictionary or list, the format is :

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

I have tried:

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

Result = 'Out of index 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]

Which seems to read each char as a column, instead of each 'word'

I would like to get the four columns, ie:

2
2343333
bana
twelve

into seperate dictionaries or lists

Any help is great, thanks!

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')]
>>> 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, 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 for getting 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']}
with  open("path\sample1.csv") as f:
    for line in f:
        print line

// to read file line by line

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 needs to grow 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 then print your columns with:

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

How about this?

f = open("file.txt")

for i in f:
    k = i.split()
    for j in k:
        print j

You could use a list comprehension like this:

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

You will then have it in a 2d array allowing you to group it any way you like it.

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