我需要解析csv
文件。CSV读取列的值
输入:文件名+
Index | writer | year | words
0 | Philip | 1994 | this is first row
1 | Heinz | 2000 | python is wonderful (new line) second line
2 | Thomas | 1993 | i don't like this
3 | Heinz | 1898 | this is another row
. | . | . | .
. | . | . | .
N | Fritz | 2014 | i hate man united
输出:对应所有单词列表来命名
l = ['python is wonderful second line', 'this is another row']
我有什么企图?
import csv
import sys
class artist:
def __init__(self, name, file):
self.file = file
self.name = name
self.list = []
def extractText(self):
with open(self.file, 'rb') as f:
reader = csv.reader(f)
temp = list(reader)
k = len(temp)
for i in range(1, k):
s = temp[i]
if s[1] == self.name:
self.list.append(str(s[3]))
if __name__ == '__main__':
# arguements
inputFile = str(sys.argv[1])
Heinz = artist('Heinz', inputFile)
Heinz.extractText()
print(Heinz.list)
输出是:
["python is wonderful\r\nsecond line", 'this is another row']
如何获取包含单词的多行细胞摆脱\r\n
,并且可以循环作为其极其缓慢得到改善呢?
这不是我想要的。我需要一个特定的作家/艺术家的话。不是所有的单词。 –
@TonyTannous更新了特定的作家答案。 –