2013-06-12 199 views
0

我正在运行Python 2.7。我对Python很陌生。我正在尝试读取CSV文件(值由空格分隔),并根据坐标上方的标题分隔内部值。该文件的格式不是我习惯的,我无法正确读取值。即使我能让他们正确阅读,我也不知道如何将它们放入列表中。使用Python读取CSV文件2

下面是CSV文件的样子:

# image name 
1.png 
# probe locations 
100 100 
200 100 
100 200 
300 300 

# another image name 
2.png 
100 200 
200 100 
300 300 
135 322 

# end 

这里是我与打码:

class CommentedFile: 
    def __init__(self, f, commentstring="#"): 
     self.f = f 
     self.commentstring = commentstring 
    def next(self): 
     line = self.f.next() 
     while line.startswith(self.commentstring): 
      line = self.f.next() 
     return line 
    def __iter__(self): 
     return self 

#I did this in order to ignore the comments in the CSV file 

tsv_file = csv.reader(CommentedFile(open("test.exp", "rb")), 
        delimiter=' ') 


for row in tsv_file: 
    if row != int: 
     next(tsv_file) 
    if row: 
     print row 

代码打印出:

['100', '100'] 
['100', '200'] 
['100', '200'] 
['300', '300'] 
Traceback (most recent call last): 
    File "the path", line 57, in <module> 
next(tsv_file) 
StopIteration 

所以我m试图让程序根据标题分开坐标,然后将它们放入单独的列表中。感谢您的帮助!

+1

那不是真的* * CSV文件,所以它可能不适合使用'CSV .reader'。示例输入文件需要的输出是什么? – Aya

+0

我不明白行'if row!= int:'??? –

+0

我最终希望使用reader/parser的输出作为我正在绘制的图形的坐标。所以我想,列表中的x和y坐标与它们在输出中的方式一起。我应该用什么来代替csv.reader? – user2479054

回答

0

看看pandas。它有一个可以保存你的数据并允许你以直观的方式进行操作的对象。它还有一个read_csv函数,它在处理csv文件时可以省去很多麻烦。

例如:

import pandas as pd 

#reads your csv file in and returns a DataFrame object as metioned above. 
df = pd.read_csv("your_csv.csv", sep=' ', names=['co_a','co_b'], header=None, skiprows=2) 

#extracts your discordant to separate lists 
list1 = df.co_a.to_list() 
list2 = df.co_b.to_list() 

可以使用dfdf.head()看到你的数据的数据帧,以及如何管理。还值得一提的是,df.co_a是一个Series对象,认为超级列表/字典,你可以从那里做你的分析或操纵。

此外,如果您向我展示csv文件中的评论如何,我可以告诉你如何用read_csv忽略它们。

我知道你一直在寻找csv module的答案,但这是一个更先进的工具,从长远来看可能会帮助你。

希望它有帮助!

+0

谢谢!该CSV文件看起来*完全*就像它在我问的问题。评论遵循标签(#)。 CSV文件在坐标上没有标题。有什么办法可以按他们的列号对它们进行排序吗?或者我需要标题?我无法真正改变CSV文件的格式。 – user2479054

+0

列号是好的,但它可能是值得添加简单名称的列,我会更新我的答案。 – agconti

+0

也可以将这两个png列表分隔成不同的文件。两者都不会正确导入数据框。如果你除了数字之外的任何东西都会被删除,那么使用上面的代码会很好。 – agconti

0

你的代码实际上对我很好。我不知道你为什么得到回溯。

tmp.csv

# image name 
1.png 
# probe locations 
100 100 
200 100 
100 200 
300 300 

# another image name 
2.png 
100 200 
200 100 
300 300 
135 322 

# end 

tmp.py

import csv 

class CommentedFile: 
    def __init__(self, f, commentstring="#"): 
     self.f = f 
     self.commentstring = commentstring 
    def next(self): 
     line = self.f.next() 
     while line.startswith(self.commentstring): 
      line = self.f.next() 
     return line 
    def __iter__(self): 
     return self 

#I did this in order to ignore the comments in the CSV file 

tsv_file = csv.reader(CommentedFile(open("tmp.csv", "rb")), 
        delimiter=' ') 


for row in tsv_file: 
    if row != int: 
     next(tsv_file) 
    if row: 
     print row 

壳牌输出

tmp$python tmp.py 
['1.png'] 
['200', '100'] 
['300', '300'] 
['2.png'] 
['200', '100'] 
['135', '322'] 
tmp$uname -mprsv 
Darwin 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64 i386 
tmp$python --version 
Python 2.7.2