그래서 나는 때때로 임의의 개행 문자가 일부 셀에 입력되는이 데이터 세트를 가지고 있으며 삭제해야합니다.
이것이 내가 시도한 것입니다.
with open ('filepath') as inf, open('filepath', 'w') as outf:
for line in inf:
outf.write(line.replace('\n', ''))
불행히도 이것은 행의 끝 부분을 포함하여 모든 개행 문자를 제거하여 내 csv 파일을 큰 한 줄로 바꿉니다.
누구든지 내가 '실제'끝줄 문자가 아닌 임의의 줄 바꿈 문자 만 삭제할 수있는 방법을 알고 있습니까?
편집 : 도움이된다면, 각 '실제'새 줄은 6 자리 숫자 문자열로 시작합니다 (헤더 줄 제외). 숫자 문자열이 있는지 미리 감지하는 정규식 패턴이 작동 할 수 있습니까?
Edit2 : pandas를 사용하여 다음으로 편집 해 보았습니다.
df = pd.read_csv(filepath)
for i in df.columns:
if df[i].dtype==np.object:
df[i] = df[i].str.replace('\n','')
이상하게도 .csv 내부의 내용을 새 텍스트 파일로 복사하면 작동하지만 원래 csv 파일에서는 작동하지 않으며 그 이유를 모르겠습니다.
최종 편집 :
그의 도움을 주신 DDS에게 큰 감사를드립니다. 이것을 사용하여 작동하도록 관리했습니다.
num_cols = 48
buf = ""
with open (filepath) as inf, open (filepath, 'w') as outf:
for line in inf:
if len(line.split(',')) < num_cols:
buf += line.replace('\n', '')
if len(buf.split(',')) == num_cols:
outf.write(buf+'\n')
else: continue
buf = ""
else:
outf.write(line)
행당 필드 수를 알고 있고 필드에 csv 구분 기호 (쉼표)가 포함되어 있지 않다고 가정하면 다음과 같이 할 수 있습니다.
number_of_columns_in_the_table = 5 #assuming a line has 5 columns
with open ('filepath') as inf, open('filepath', 'w') as outf:
for line in inf:
#check if the number of "splits equals the nummber of fields"
if len(line.split(',')) < number_of_columns_in_the_table
outf.write(line.replace('\n', ''))
else:
outf.write(line)
편집하다
number_of_columns_in_the_table = 5 #assuming a line has 5 columns
with open ('filepath') as inf, open('filepath', 'w') as outf:
for line in inf:
#check if the number of "splits equals the nummber of fields"
if len(line.split(',')) < number_of_columns_in_the_table
buf += line.replace('\n', '');
if len(line.split(',')) == number_of_columns_in_the_table
outf.write( buf)
else:
outf.write(line)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다