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)
출력은 다음과 같습니다 루프가 매우 느린로 나는 단어 하나 개 이상의 라인을 포함하는 셀에 대한 \r\n
제거하려면 어떻게
["python is wonderful\r\nsecond line", 'this is another row']
등을 개선 할 수 있을까?
:
–@TonyTannous 특정 작성자의 답변을 업데이트했습니다. –