반응형
다른 파일에서 몇 줄로 작성한 다음 데이터에서 일부 개체를 만드는 임시 파일을 만들려고합니다. 읽을 수 있도록 임시 파일을 찾아서 여는 방법을 모르겠습니다. 내 코드 :
with tempfile.TemporaryFile() as tmp:
lines = open(file1).readlines()
tmp.writelines(lines[2:-1])
dependencyList = []
for line in tmp:
groupId = textwrap.dedent(line.split(':')[0])
artifactId = line.split(':')[1]
version = line.split(':')[3]
scope = str.strip(line.split(':')[4])
dependencyObject = depenObj(groupId, artifactId, version, scope)
dependencyList.append(dependencyObject)
tmp.close()
본질적으로 저는 실수로 파일을 덮어 쓰지 않도록 보호하기 위해 중개자 임시 문서를 만들고 싶습니다.
해결 방법
with tempfile.TemporaryFile() as tmp:
lines = open(file1).readlines()
tmp.writelines(lines[2:-1])
tmp.seek(0)
for line in tmp:
groupId = textwrap.dedent(line.split(':')[0])
artifactId = line.split(':')[1]
version = line.split(':')[3]
scope = str.strip(line.split(':')[4])
dependencyObject = depenObj(groupId, artifactId, version, scope)
dependencyList.append(dependencyObject)
참조 페이지 https://stackoverflow.com/questions/39983886
반응형
'파이썬' 카테고리의 다른 글
파이썬 AttributeError : 'urllib'모듈에 'urlopen'속성이 없습니다. (0) | 2020.10.29 |
---|---|
파이썬 Truthy와 Falsy는 무엇입니까? 참 및 거짓과 어떻게 다릅니 까? (0) | 2020.10.29 |
파이썬 Eclipse에서 pep8.py를 통합하는 방법은 무엇입니까? (0) | 2020.10.28 |
파이썬 Pandas를 사용하여 데이터 프레임에 빈 행 추가 (0) | 2020.10.28 |
파이썬 JSON에서 바이트를 인코딩하는 방법은 무엇입니까? TypeError를 던지는 json.dumps () (0) | 2020.10.28 |
댓글