I'm attempting to load a tab delimited text file into a python program. It has the following format,
AAAAAA 1234 5678 90AB QQQQ JKL1
BBBBBB QWER TYUI ASDF QQQQ
CCCCCC ZXCV 1234 PPPP
...
ZZZZZZ 1111
In short, variable numbers of columns for each row, but always at least two and each column within a row is unique. The first column I would like to use as a key, and load the rest into a list with the key pointing to it. I tried looking into the csv module already as was suggested in other threads, but I've not quite found a way to make it work for me. So yeah, apologies if this should be more obvious, very much a newbie question.
simple str.split
should work just fine for splitting the columns. Using that, you just need to read each row and split it into columns taking the first element as the key and the rest as the value:
with open(file) as fin:
rows = ( line.split('\t') for line in fin )
d = { row[0]:row[1:] for row in rows }
import csv
d = {}
with open('tab_delimited_file.txt', 'rb') as csv_file:
for row in csv.reader(csv_file, delimiter='\t'):
d[row[0]] = row[1:]
print(d)
Output:
{'AAAAAA': ['1234', '5678', '90AB', 'QQQQ', 'JKL1'],
'CCCCCC': ['ZXCV', '1234', 'PPPP'],
'BBBBBB': ['QWER', 'TYUI', 'ASDF', 'QQQQ'],
'ZZZZZZ': ['1111']}
>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
... spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
... for row in spamreader:
... print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
This is just an example of how you can take a delimited file, and print the row data. Obviously this can be expanded and put into a dict or a list or something..
More info (and where this was taken from): http://docs.python.org/2/library/csv.html
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.