简体   繁体   English

如何使用空格分隔值逐行读取文件?

[英]How to read a file line by line with space separated values?

I have a different kind of file format which contains millions of lines in a txt file.我有一种不同类型的文件格式,它在 txt 文件中包含数百万行。

My file format is something like this:我的文件格式是这样的:

12122.AA.K IRIR-93I3KD-OEPE-IE,6373,893939,09/12/2093,,N,EC,3838-38939-393
12123.AA.K KKKS-93I3KD-OEPE-IE,9393,039033,09/12/2093,,N,EC,3838-38939-393
12122.AA.K PEOEP-93I3KD-OEPE-IE,9033,930392,09/12/2093,,N,EC,3838-38939-393
12124.AA.K MDJDK-93I3KD-OEPE-IE,3930,272882,09/12/2093,,N,EC,3838-38939-393
12125.AA.K EOEPE-93I3KD-OEPE-IE,8393,039393,09/12/2093,,N,EC,3838-38939-393

In Python, I want to split each line into a key and a value:在 Python 中,我想将每一行拆分为一个键和一个值:

Key: 12122.AA.K
Value: IRIR-93I3KD-OEPE-IE,3833,343343,09/12/2093,,N,EC,3838-38939-393

As you can see, the key and value are differentiated by one empty space only.如您所见,键和值仅通过一个空格来区分。

What's the efficient way of getting in python?进入python的有效方法是什么?

with open(filename) as f:
    mapping = dict(line.split(' ', 1) for line in f) 
with open('file.txt','r') as file:
   thedict={e.split(' ')[0]:e.split(' ')[1] for e in file}

You could try this dictionary comprehension你可以试试这个词典理解

It's going to be overkill, but you can also use the built-in csv module.这将是矫枉过正,但您也可以使用内置的csv模块。

While it's designed to work for comma-separated values by default, it does provide a way toregister a custom dialect to match custom file formats, such as files with space-separated values.虽然默认情况下它设计用于逗号分隔值,但它确实提供了一种注册自定义方言以匹配自定义文件格式(例如具有空格分隔值的文件)的方法。 The Dialect and Formatting Parameters includes an attribute for a delimiter which you can set to a space " " . 方言和格式参数包括一个delimiter属性,您可以将其设置为空格" "

import csv
from pprint import pprint

csv.register_dialect("my_custom_dialect", delimiter=" ")

mapping1 = {}
with open("test.txt") as f:
    reader = csv.reader(f, dialect="my_custom_dialect")
    for row in reader:
        # Each row is a list of strings separated by the delimiter
        key, value = row
        mapping1[key] = value
pprint(mapping1)
{'12122.AA.K': 'IRIR-93I3KD-OEPE-IE,6373,893939,09/12/2093,,N,EC,3838-38939-393',
 '12123.AA.K': 'KKKS-93I3KD-OEPE-IE,9393,039033,09/12/2093,,N,EC,3838-38939-393',
 '12124.AA.K': 'PEOEP-93I3KD-OEPE-IE,9033,930392,09/12/2093,,N,EC,3838-38939-393',
 '12125.AA.K': 'MDJDK-93I3KD-OEPE-IE,3930,272882,09/12/2093,,N,EC,3838-38939-393',
 '12126.AA.K': 'EOEPE-93I3KD-OEPE-IE,8393,039393,09/12/2093,,N,EC,3838-38939-393'}

If your file has headers, then you can leverage csv 'sDictReader to access each row's values as a dict.如果您的文件有标题,那么您可以利用csvDictReader将每一行的值作为字典访问。

KEY VALUE
12122.AA.K IRIR-93I3KD-OEPE-IE,6373,893939,09/12/2093,,N,EC,3838-38939-393
12123.AA.K KKKS-93I3KD-OEPE-IE,9393,039033,09/12/2093,,N,EC,3838-38939-393
12124.AA.K PEOEP-93I3KD-OEPE-IE,9033,930392,09/12/2093,,N,EC,3838-38939-393
import csv
from pprint import pprint

csv.register_dialect("my_custom_dialect", delimiter=" ")

mapping2 = {}
with open("test_with_headers.txt") as f:
    reader = csv.DictReader(f, dialect="my_custom_dialect")
    for row in reader:
        # 'row' is a dictionary with the headers as the key
        mapping2[row["KEY"]] = row["VALUE"]
pprint(mapping2)
{'12122.AA.K': 'IRIR-93I3KD-OEPE-IE,6373,893939,09/12/2093,,N,EC,3838-38939-393',
 '12123.AA.K': 'KKKS-93I3KD-OEPE-IE,9393,039033,09/12/2093,,N,EC,3838-38939-393',
 '12124.AA.K': 'PEOEP-93I3KD-OEPE-IE,9033,930392,09/12/2093,,N,EC,3838-38939-393'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM