我需要解析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更新了特定的作家答案。 –