使用Python 2.6的CSV问题 [英] CSV Problem using Python 2.6

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

问题描述

我对python很新,我正在做一个项目,我需要读取一个CSV文件并比较文件每一行的列。如果一行中的列与另一行中的列具有相同的值,则将这些行写入同一文件,如果它们不同则将它们写入不同的文件中......所以基本上任何具有相同列的行都会被写入到同一个CSV文件。



这是我到目前为止在代码方面的内容



import csv


file = csv.reader(open(''C:/NEW.CSV'',''r''),delimiter ='','')

first = csv.writer(open(''C:/First.CSV'',''w''))

second = csv.writer(open(''C) :/fir.CSV'',''w''))


header = file.next()#ignore标题


>>> temp = 0

>>>对于文件中的行:

...

...如果temp == row [4]:#这是我需要为每行比较的列

... first.writerow(行)

...其他:

... second.writerow(行)

...

... temp = row [4]

...


写入a CSV文件很好,但它不会将相同的行写入同一个CSV。

我知道问题在于我的分配温度等于行[4]。我相信我的缩进是错的,我尝试了一切,但是当我甚至尝试了一个简单的程序时:


>>> x = 5

>>>如果x == 5:

...打印''x是5''

...否则:

...打印''x不是5''

...

... print''无论x是否为5都应该打印出来''

Traceback(文件"<互动输入>",第6行

print''无论x是否为5,都应该打印出来''

^

语法错误:语法无效

>>>


i得到错误。我不知道如何缩进因为他们没有大括号,我认为我的缩进是正确的。

Hi, I am fairly new to python and I am doing a project where i need to read a CSV file and compare the columns from each row of a file. If the columns from one row has the same value as the column from another row then write those row''s to the same file, if they are different then write them into different files.. so essentially any rows with the same column gets written to the same CSV file.


Here is what I have so far in terms of code


import csv

file = csv.reader(open(''C:/NEW.CSV'',''r''), delimiter='','')
first = csv.writer(open(''C:/First.CSV'',''w''))
second = csv.writer(open(''C:/fir.CSV'',''w''))

header = file.next() #ignore the header

>>> temp = 0
>>> for row in file:
...
... if temp == row[4]: #this is the column i need to compare for each row
... first.writerow(row)
... else:
... second.writerow(row)
...
... temp = row[4]
...

It writes to a CSV file fine but it doesnt write the same rows to the same CSV.
I know the problem is with my assiging temp equal to row[4]. I believe my indentation is wrong, and i tried everything, but when i even tried a simple program for ex:

>>> x = 5
>>> if x ==5:
... print ''x is 5''
... else:
... print ''x is not 5''
...
... print '' this should get printed regardless if x is 5 or not''
Traceback ( File "<interactive input>", line 6
print '' this should get printed regardless if x is 5 or not''
^
SyntaxError: invalid syntax
>>>

i get that error. Im not sure how to indent as their are no braces and i would think that my indentation is correct.

推荐答案

发送一些示例csv文件并发布你的代码。
Send some example csv files and post up your code.


我发布了我的代码

I did post my code

展开 | 选择 | Wrap | 行号


tran0191,


发布代码时请使用代码标签。请参阅发布指南此处


如果您要发布您的数据的代表性样本和所需输出的样本,我们将更容易为您的问题提供解决方案。


BV - 主持人
tran0191,

Please use code tags when posting code. See posting guidelines here.

It would be much easier for us to offer a solution to your problem if you would post representative samples of your data and a sample of the required output.

BV - Moderator


这篇关于使用Python 2.6的CSV问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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