저는 csv 파일을 파이썬 데이터 프레임으로 변환하고 있습니다. 그리고 원본 파일에서 열 중 하나에 em-dash라는 문자가 있습니다. 하이픈 "-"으로 바꿔주세요. CSV에서파이썬에서 em-dash를 하이픈으로 변환합니다.
부분 원본 파일 :
이NoDemande NoUsager Sens IdVehicule NoConduteur HeureDebutTrajet HeureArriveeSurSite HeureEffective'
42192001801 42192002715 — 157Véh 42192000153 ...
42192000003 42192002021 + 157Véh 42192000002 ...
42192001833 42192000485 — 324My3FVéh 42192000157 ...
내 코드 :
#coding=latin-1
import pandas as pd
import glob
pd.set_option('expand_frame_repr', False)
path = r'D:\Python27\mypfe\data_test'
allFiles = glob.glob(path + "/*.csv")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
df = pd.read_csv(file_,index_col=None,header=0,sep=';',parse_dates=['HeureDebutTrajet','HeureArriveeSurSite','HeureEffective'],
dayfirst=True)
df['Sens'].replace(u"\u2014","-",inplace=True,regex=True)
list_.append(df)
그리고 그것은, 전혀처럼 보이는 그것은 단지 ?
로 변환 할 때마다 작동하지 않습니다 :
42191001122 42191002244 ? 181Véh 42191000114 ...
42191001293 42191001203 ? 319M9pVéh 42191000125 ...
42191000700 42191000272 ? 183Véh 42191000072 ...
파일에 프랑스어 문자가 있기 때문에을 사용하고 있습니다. utf-8
대신내가 첫 번째 라인을 삭제하고 다음과 같이 작성하는 경우 :
df = pd.read_csv(file_,index_col=None,header=0,sep=';',encoding='windows-1252',parse_dates=['HeureDebutTrajet','HeureArriveeSurSite','HeureEffective'],
dayfirst=True)
결과는 다음과 같습니다
42191001122 42191002244 â?? 181Véh 42191000114 ...
42191001293 42191001203 â?? 319M9pVéh 42191000125 ...
42191000700 42191000272 â?? 183Véh 42191000072 ...
가 어떻게 -
로 대체 모든 전각 대시 —
을 만들 수 있습니까? 그래서
'"42191002384";"42191000118";"\xe2\x80\x94";"";"42191000182";...
'"42191002464";"42191001671";"+";"";"42191000182";...
'"42191000045";"42191000176";"\xe2\x80\x94";"620M9pV\xc3\xa9h";"42191000003";...
'"42191001305";"42191000823";"\xe2\x80\x94";"310V7pV\xc3\xa9h";"42191000126";...
'repr' 출력은 실제 파일의 행이 아닌 파일 이름의 문자를 반복하므로 매우 유용하지는 않습니다. 더 유용한 결과를 얻기 위해서는'for line in open (file_)'을 사용하십시오. 출력물에서 몇 가지 예제 라인을 선택하여 질문을 추가하는 대신 모든 질문에 추가하십시오. – Blckknght