with open('D:\Scripting Test/Full Data.txt') as f:
for line in f:
with open('D:\Scripting Test/Numbers.txt') as ff:
for linee in ff:
if line.startswith(linee):
print(line)
i want to print all lines in file (full-data.txt) if they start with any numbers in file (numbers.txt)
full-data.txt:
4/0/0
3/0/7
4/0/7
4/0/3
4/0/4
4/0/1
3/0/5
3/0/1
2/0/5
2/0/3
2/0/4
2/0/6
3/0/2
1/0/3
6/0/6
6/0/12
1/0/5
1/0/4
3/0/4
Numbers.txt:
1
2
5
8
output should be:
2/0/5
2/0/3
2/0/4
2/0/6
1/0/3
1/0/5
1/0/4
Your issue is that when you read in some of the numbers, they include whitespace like newline characters and spaces. So try first reading in all of the numbers into an array and stripping any whitespace away:
nums = []
with open('D:\\Scripting Test\\Numbers.txt') as f:
nums = f.read().split('\n')
nums = [n.strip() for n in nums if n != ''] # strip whitespace and get rid of empty lines
with open('D:\\Scripting Test\\Full Data.txt') as f:
for line in f:
for num in nums:
if line.startswith(num):
print(line)