문제 설명
Enthought Canopy의 Python: csv 파일 조작 (Python in Enthought Canopy: manipulating csv files)
내 코드의 목표는 csv 파일을 가져와서 좌표 값이 포함된 열만 가져와서 배열로 변환하고 다른 파일의 다른 좌표 집합으로 수학 함수를 수행할 수 있도록 하는 것입니다. 파이썬에서 파일 조작은 제 강점이 아닙니다. 내 코드는 다음과 같습니다.
list1 = []
DR12 = open('dr12data.csv', 'r')
for line in DR12:
linelist = line.split(',')
list1.append(linelist)
이렇게 하면 파일의 각 줄이 내가 원하는 개별 값을 선택할 수 있는 2차원 목록으로 바뀝니다. 그러나 한 번에 둘 이상의 값을 선택할 수 없으며 별도의 파일이나 목록에 좌표를 배치하려면 한 줄씩 수행해야 합니다(좀 더 무차별적인 방법). 코드를 보다 효율적이고 일반적으로 만들려고 합니다.
참조 솔루션
방법 1:
You could use pandas
package which is written with C so it's fast and use read_csv
with usecols
to specify column which you need:
import pandas as pd
df = pd.read_csv('your_file', usecols=['col1', 'col2'])
Then you could use a lot of pantmethods, do mathematical function with whole columns.
(by Eric Kopins、Anton Protopopov)