使用python从csv解析单个列到文本文件 [英] Parsing out single column from csv into text file using python

查看:156
本文介绍了使用python从csv解析单个列到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于得到我的dbf文件是一个csv,但现在我很困惑,如何解析为一个文本文件进一步编辑。



一直在读csv模块,但老实说,我的头开始旋转。这么多对我来说似乎是希腊。



我的car.csv文件看起来像这样:

 名称,总数,频率
丰田,2,2
马自达,1,1
起亚,2,1
Volkswagon, 3,1

我想将以下句子输出到文本文件(或csv) p>

在这个社区内有一辆丰田,马自达,起亚和大众汽车停在街上。



是两个我不想要的逗号:



Cars.dbf

 丰田,2,2 
马自达,2,1

Cars.dbf

 

这个街区内的街道上没有停放的汽车。



我从前一篇文章中得到了一个很好的建议,就是构造if-else语句,但是却失去了如何解析列(由于某种原因,它们没有显示在Excel中,我认为我只有一列)



python 2.7



任何人都知道一个好的网站,解释csv模块在一个总的新手可以理解的术语?感谢。

解决方案

Python文档示例总是一个地方。



你想要这样的:

  import csv 

makes = []
with open('cars.csv','rb')as f:
reader = csv.reader(f)
next(reader)#忽略第一行

读取器中的行:
makes.append(row [0 ])

print make


I finally got my dbf file to be a csv but now I am confused as to how to parse this out into a text file for further editing.

I have been reading up on the csv module but to be honest my head began to spin. So much of it seemed Greek to me. However, I would like the code for using module this if possible.

My car.csv file looks like this:

Name,Total,Freq
Toyota,2,2
Mazda,1,1
Kia,2,1
Volkswagon,3,1

I want to output the following sentence into a text file (or csv):

Within this neighborhood there is a Toyota, Mazda, Kia, and Volkswagon parked on the street.

If the results are two I do not want commas:

Cars.dbf

Toyota,2,2
Mazda,2,1

Within this neighborhood there is a Toyota and Mazda parked on the street.

Cars.dbf

empty

There are no cars parked on the street within this neighborhood.

I got a good suggestion from a previous post on constructing the if-else sentences but am lost on how to parse columns (they didn't show up in Excel for some reason, I thought I only had one column)

python 2.7

BTW. Anyone know of a good web site that explains the csv module in terms a total newbie can understand? Thanks.

解决方案

The Python doc examples are always a go to place.

You want something like this:

import csv

makes = []
with open('cars.csv', 'rb') as f:
    reader = csv.reader(f)
    next(reader) # Ignore first row

    for row in reader:
        makes.append(row[0])

print makes

这篇关于使用python从csv解析单个列到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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