I have a huge list of python packages that had been installed saved with the version numbers in the file "foo.txt", I want to delete the "==" and whatever after that in each lines, and save the file.
example text in the file:
autopep8==1.5.3
beautifulsoup4==4.8.2
bleach==3.1.4
bumpversion==0.5.3
... etc
Use the below code.
read that file and store it in list named pkg_list.
output_lst = []
with open("input.txt", "r") as f:
input_data = f.readlines()
output_lst = [name.split("==")[0] for name in input_data]
with open("input.txt", "w") as f:
for pkg in output_lst:
f.write("{}\n".format(pkg))
Use this line of code if you want to create a new file with updated data, if you want to just read use the data then make changes accordingly.
def read_write_file(input_path, output_path):
with open(input_path, "r") as input_file:
content = input_file.readlines()
with open(output_path, 'w') as output_file:
for line in content:
line = line[:line.find('==')]
output_file.write(line + '\n')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.