Dec-27-2019, 08:02 AM
You are doing two task here making a list and wirte to file.
Let say this is
Let say this is
sample.txt
.Output:203.215.181.219:36342
200.149.0.74:8080
46.209.98.227:8080
150.95.131.174:3128
Now just do the witting to file part and it seems okay.with open('sample.txt', 'r') as fin: data = fin.read().splitlines(True) with open('sample1.txt', 'w') as fout: fout.writelines(data[2:])Other name
sample1.txt
so it don't overwrite original in this test.Output:46.209.98.227:8080
150.95.131.174:3128
Getting a list without \n
could be done like this.with open('sample.txt', 'r') as fin: data = [i.strip() for i in fin]
Output:>>> data
['203.215.181.219:36342',
'200.149.0.74:8080',
'46.209.98.227:8080',
'150.95.131.174:3128']
Writing back from this list could be.with open('sample.txt', 'r') as fin: data = [i.strip() for i in fin] with open('sample2.txt', 'w') as fout: for ip in data[:2]: fout.write(f'{ip}\n')
Output:203.215.181.219:36342
200.149.0.74:8080