今天我再跑一個開源的項目,人家給我了一個環(huán)境列表,長下面這個樣子:
allennlp 2.6.0
anykeystore 0.2
argon2-cffi 20.1.0
astor 0.8.1
astunparse 1.6.3
async-generator 1.10
async-timeout 3.0.1
attrs 19.3.0
backcall 0.1.0
backports-csv 1.0.7
我想要把他變成下面這個樣子,然后放到虛擬機中去批量下載這些包:
_libgcc_mutex==0.1
absl-py==0.13.0
aiohttp==3.7.4.post0
allennlp==2.6.0
anykeystore==0.2
argon2-cffi==20.1.0
astor==0.8.1
astunparse==1.6.3
async-generator==1.10
詳情見代碼
# 逐行讀取txt文件
if __name__=='__main__':
with open('requirement.txt','r',encoding='utf-8')as f:
content=f.readlines()
package_names=[]
for line in content:
txt=line.strip().split(" ")
package_name=txt[0]+"=="+txt[-1]
package_names.append(package_name)
print(package_name)
# 逐行寫入txt文件
with open('requirements.txt','w',encoding='utf-8')as f:
for i in package_names:
f.write(i+'\n')