Python导入csv列表

我有一个约2000个logging的CSV文件。

每个logging都有一个string和一个类别。

This is the first line, Line1 This is the second line, Line2 This is the third line, Line3 

我需要将这个文件读入一个看起来像这样的列表;

 List = [('This is the first line', 'Line1'), ('This is the second line', 'Line2'), ('This is the third line', 'Line3')] 

如何可以将此csv导入我需要使用Python的列表?

使用csv模块(Python 2.x):

 import csv with open('file.csv', 'rb') as f: reader = csv.reader(f) your_list = list(reader) print your_list # [['This is the first line', 'Line1'], # ['This is the second line', 'Line2'], # ['This is the third line', 'Line3']] 

如果你需要元组:

 import csv with open('test.csv', 'rb') as f: reader = csv.reader(f) your_list = map(tuple, reader) print your_list # [('This is the first line', ' Line1'), # ('This is the second line', ' Line2'), # ('This is the third line', ' Line3')] 

Python 3.x版本(由@seokhoonlee下面)

 import csv with open('file.csv', 'r') as f: reader = csv.reader(f) your_list = list(reader) print(your_list) # [['This is the first line', 'Line1'], # ['This is the second line', 'Line2'], # ['This is the third line', 'Line3']] 

Python3更新:

 import csv with open('file.csv', 'r') as f: reader = csv.reader(f) your_list = list(reader) print(your_list) # [['This is the first line', 'Line1'], # ['This is the second line', 'Line2'], # ['This is the third line', 'Line3']] 

pandas在处理数据方面相当不错。 以下是一个如何使用它的例子:

 import pandas as pd # Read the CSV into a pandas data frame (df) # With a df you can do many things # most important: visualize data with Seaborn df = pd.read_csv('filename.csv', delimiter=',') # Or export it in many ways, eg a list of tuples tuples = [tuple(x) for x in df.values] # or export it as a list of dicts dicts = df.to_dict().values() 

一个很大的优势就是pandas可以自动处理标题行。

如果你还没有听说过Seaborn ,我build议你看一看。

另请参阅: 如何使用Python读取和写入CSV文件?

如果您确定input中没有逗号,除了分开类别之外,您可以逐行读取文件并分开 ,然后将结果推送到List

也就是说,看起来你正在查看一个CSV文件,所以你可能会考虑使用它的模块

 result = [] for line in text.splitlines(): result.append(tuple(line.split(","))) 

一个简单的循环就足够了:

 lines = [] with open('test.txt', 'r') as f: for line in f.readlines(): l,name = line.strip().split(',') lines.append((l,name)) print lines 

稍微扩展一下你的需求,假设你不关心行的顺序,想把它们分类到下面,下面的解决scheme可以为你工作:

 >>> fname = "lines.txt" >>> from collections import defaultdict >>> dct = defaultdict(list) >>> with open(fname) as f: ... for line in f: ... text, cat = line.rstrip("\n").split(",", 1) ... dct[cat].append(text) ... >>> dct defaultdict(<type 'list'>, {' CatA': ['This is the first line', 'This is the another line'], ' CatC': ['This is the third line'], ' CatB': ['This is the second line', 'This is the last line']}) 

通过这种方式,您可以在字典下的所有关键字中find相关的行。

接下来是一段代码,它使用csv模块,但提取file.csv内容到一个列表使用的第一行是csv表头

 import csv def csv2dicts(filename): with open(filename, 'rb') as f: reader = csv.reader(f) lines = list(reader) if len(lines) < 2: return None names = lines[0] if len(names) < 1: return None dicts = [] for values in lines[1:]: if len(values) != len(names): return None d = {} for i,_ in enumerate(names): d[names[i]] = values[i] dicts.append(d) return dicts return None if __name__ == '__main__': your_list = csv2dicts('file.csv') print your_list