使用Python解析CSV /制表符分隔的txt文件 [英] Parsing CSV / tab-delimited txt file with Python

查看:1790
本文介绍了使用Python解析CSV /制表符分隔的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个CSV档案,在Excel中开启时,共有5栏。只有A列和C列对我有任何意义,其余列中的数据是不相关的。

I currently have a CSV file which, when opened in Excel, has a total of 5 columns. Only columns A and C are of any significance to me and the data in the remaining columns is irrelevant.

从第8行开始,然后以7的倍数工作。行8,15,22,29,36等...),我正在寻找一个字典与Python 2.7与这些字段的信息。 A列中的数据将是密钥(6位整数),C列中的数据是密钥的相应值。我尝试在下面突出显示,但格式不是最好的: -

Starting on line 8 and then working in multiples of 7 (ie. lines 8, 15, 22, 29, 36 etc...), I am looking to create a dictionary with Python 2.7 with the information from these fields. The data in column A will be the key (a 6-digit integer) and the data in column C being the respective value for the key. I've tried to highlight this below but the formatting isn't the best:-

    A        B      C          D
1                           CDCDCDCD  
2                           VDDBDDB
3
4
5
6
7  DDEFEEF                   FEFEFEFE
8  123456         JONES
9
10
11
12
13
14
15 293849         SMITH

根据上面的内容,我想从我的字典中提取值A7(DDEFEEF)作为键,FEFEFEFE作为相应的数据,然后添加另一个条目到我的字典,跳到第15行2938495是我的关键,史密斯是相应的值。

As per the above, I am looking to extract the value from A7 (DDEFEEF) as a key in my dictionary and "FEFEFEFE" being the respective data and then add another entry to my dictionary, jumping to line 15 with "2938495" being my key and "Smith" being the respective value.

有任何建议吗?源文件是一个.txt文件,其中条目用制表符分隔。
感谢

Any suggestions? The source file is a .txt file with entries being tab-delimited. Thanks

澄清:

为了澄清,到目前为止, -

Just to clarify, so far, I have tried the below:-

import csv

mydict = {:}
f = open("myfile", 'rt')
reader = csv.reader(f)
    for row in reader:
        print row

上面的代码只是一行一行地打印所有内容。我没有尝试为行(7)在阅读器但这返回一个错误。然后我研究了它,并在下面走了,但它没有工作两个:

The above simply prints out all content though a row at a time. I did try "for row(7) in reader" but this returned an error. I then researched it and had a go at the below but it didn't work neither:

import csv
from itertools import islice

entries = csv.reader(open("myfile", 'rb'))
mydict = {'key' : 'value'}

for i in xrange(6):
    mydict['i(0)] = 'I(2)    # integers representing columns
    range = islice(entries,6)
    for entry in range:
        mydict[entries(0) = entries(2)] # integers representing columns


推荐答案

首先将文本转换为列表列表。这将处理解析部分:

Start by turning the text into a list of lists. That will take care of the parsing part:

lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t'))

其余的可以通过索引查找:

The rest can be done with indexed lookups:

d = dict()
key = lol[6][0]      # cell A7
value = lol[6][3]    # cell D7
d[key] = value       # add the entry to the dictionary
 ...

这篇关于使用Python解析CSV /制表符分隔的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆