2017-09-30 4 views
0

웹 사이트의 일부 데이터를 스크랩하고 정보를 목록 목록에 저장하고 있습니다. 각 목록은 8 개의 짧은 문자열 (최대 30 자)로 구성된 9 개의 문자열 요소로 구성됩니다. 그러나 각 목록의 마지막 요소는 길이가 200자를 넘는 상대적으로 긴 문자열이며 최대 1000 자까지 다양합니다. 각 목록을 CSV 파일에 쓰려고하면 모든 요소가 문제없이 마지막 요소를 제외하고 쓰여집니다. 지식이 부족하기 때문에 문제는 마지막 요소의 길이라고 추측 할 수는 있지만 그것을 증명할 방법이 없습니다. 내가 터미널에서 하나의 목록을 인쇄 할 때Python - CSV 파일에 상대적으로 긴 문자열을 쓸 수 없습니다.

, 나는 출력과 같이 얻을 : CSV 파일에 기록되고 완전히 때

['BSM Crew Service Centre – Croatia', 'http://maritime-connector.com/company/bsm-crew-service-centre-croatia/147/', 'C/E', 'http://maritime-connector.com/job/bsm-crew-service-centre-croatia-ce-3513/', 'Engine', 'Container ship', 'Worldwide', '19.12.201', '\nContract: 4 months onboard\t\t\t\t\t\t\tWith a fleet of approximately 650 vessels under full and crew management we offer excellent career opportunities and steady employment for professional, capable and ambitious people onboard and onshore. Recognizing that people will always be our most valuable asset, we concentrate not only on recruitment and training, but also on maintaining highly motivated staff in every position. The key here is a long-term approach based on excellent human resources practices. Our tough but fair selection procedures, periodic appraisals, performance-based incentives, timely payments and opportunities for development all play a crucial role in maintaining an excellent pool of dedicated office personnel and seafarers all around the world\r\n\t\t\t\t\t\t'] 

마지막 요소가 생략됩니다. CSV 대신 다른 유형의 파일에 목록을 쓰려고 생각했지만 대체 방법에 대한 정보가 없습니다. csv 파일로 작성을위한

내 코드는 다음과 같습니다 : 나는 문제가 될 수있는 것을 볼 수 없습니다

with open('Job-Listing.csv', 'w', encoding='utf-8') as outputCSV: 
     jobListingCSV = csv.writer(outputCSV, dialect = 'excel', \ 
            lineterminator = '\n', \ 
            delimiter = ';') 
     for post in self.JobPost: 
     # self.JobPost is a collection of lists (post) which contain strings 
      jobListingCSV.writerow(post) 

. '\ n'과 '\ t'문자로 CSV 라이터가 각 목록의 마지막 요소를 건너 뛸 수 있으므로 인코딩을 시도해 보았지만 성공하지 못했습니다.

, 최소한의 완전하고 검증 가능한 예 :

import csv 

JobPost =[['BSM Crew Service Centre – Croatia', 'http://maritime-connector.com/company/bsm-crew-service-centre-croatia/147/', 'C/E', 'http://maritime-connector.com/job/bsm-crew-service-centre-croatia-ce-3513/', 'Engine', 'Container ship', 'Worldwide', '19.12.201', '\nContract: 4 months onboard\t\t\t\t\t\t\tWith a fleet of approximately 650 vessels under full and crew management we offer excellent career opportunities and steady employment for professional, capable and ambitious people onboard and onshore. Recognizing that people will always be our most valuable asset, we concentrate not only on recruitment and training, but also on maintaining highly motivated staff in every position. The key here is a long-term approach based on excellent human resources practices. Our tough but fair selection procedures, periodic appraisals, performance-based incentives, timely payments and opportunities for development all play a crucial role in maintaining an excellent pool of dedicated office personnel and seafarers all around the world\r\n\t\t\t\t\t\t'],['Columbia Shipmanagement Rijeka', 'http://maritime-connector.com/company/columbia-shipmanagement-rijeka/1251/', '1 x CHIEF ENGINEER FOR HEAVY LIFT VESSEL', 'http://maritime-connector.com/job/columbia-shipmanagement-rijeka-1-x-chief-engineer-for-heavy-lift-vessel-3769/', 'Engine', 'Heavy lift vessel', '', '07.09.201', '\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWe are looking for the Chief Engineer for heavy lift vessel.\r\nEngine: Sulzer 7RT- flex50 11620kW\r\n\r\nFor more info please contact Columbia Shipmanagement Rijeka d.o.o. – www.csmhr.com\r\n\t\t\t\t\t\t']] 
with open('Job-Listing2.csv', 'w', encoding='utf-8') as outputCSV: 
     jobListingCSV = csv.writer(outputCSV, dialect = 'excel', \ 
            lineterminator = '\n', \ 
            delimiter = ';') 

     for post in JobPost: 
      jobListingCSV.writerow(post) 
+0

'jobListingCSV.write (''. join (self.JobPost))'? – Torxed

+1

'lineterminator = '\ n''을 삭제하고'open' 호출 (python 3)에'newline = ""'을 사용합니다. 또한 '방언'을 삭제할 수 있습니다. 간단한 시작. 그것이 효과가 없을 이유가 없습니다. 입력 (목록)과 출력 (파일)을 작은 [mcve]로 제공 할 수 있습니까? –

+0

방금 ​​방언을 삭제하려고 시도했는데'lineterminator = '\ n''을'newline = ""'로 바꿨고 TypeError가 나타납니다 : TypeError :'newline '은이 함수의 잘못된 키워드 인수입니다 – 917k

답변

0

문제는 내가 완전히 기록 된 정보를 놓친 나는 인해 열 내부의 줄 바꿈에 완전히 기록 된 셀을보고하지 않았다이었다.

관련 문제