Here is a brief summary of my aims. I have a list of data in the data text file that are basically names or identifiers. The list of names is all on one line and seperated by a space. I want to make each data a seperate line. These data are identifiers. If for instance one name from the original data text file in also present in the big file I want to have that line of data in the big file, i.e. the name and some additional information all on the same line written to a smaller data file.
This is the program that I have started to attempt such a feat. Perhaps this is pushing the limits of my skills but I hope to be able to complete this.
datafile = open ('C:\\datatext.txt', 'r')
line = [item for item in open('C:\\datatext.txt', 'r').read().split(' ')
if item.startswith("name") or item.startswith("name2")]
line_list = line.split(" ")
completedataset = open('C:\\bigfile.txt', 'r')
smallerdataset = open('C:\\smallerdataset.txt', 'w')
trials = [ line_list ]
for line in completedataset:
for t in trials:
if t in line:
smallerdataset.write(line)
completedataset.close()
smallerdataset.close()
Here is the error that i receive when i run the program in python:
Traceback (most recent call last):
File "C:/program3.py", line 7, in <module>
line_list = line.split(" ")
AttributeError: 'list' object has no attribute 'split'
I have tried to be very thourough and look forward to your comments. If you have additional questions I will elaborate as needed promptly. All the best and enjoy the rainy weather.
EDIT:
I have made some changes to the program based on suggestions. I have this as my program now:
with open('C:\\datatext.txt', 'r') as datafile:
lines = datafile.read().split(' ')
matchedLines = [item for item in lines if item.startswith("name1") or item.startswith("othername")]
completedataset = open('C:\\bigfile.txt', 'r')
smallerdataset = open('C:\\smallerdataset.txt', 'w')
trials = [ matchedLines ]
for line in completedataset:
for t in trials:
if t in line:
smallerdataset.write(line)
completedataset.close()
smallerdataset.close()
and i'm getting this error now:
Traceback (most recent call last):
File "C:/program5.py", line 17, in
if t in line:
TypeError: 'in ' requires string as left operand, not list
>>>
Thank you for you're continued help in this matter.
EDIT 2:
I have made several changes and now I'm getting this error:
Traceback (most recent call last):
File "C:/program6.py", line 9, in
open('C:\\smallerdataset.txt', 'w')) as (completedataset, smallerdataset):
AttributeError: 'tuple' object has no attribute '__exit__'
Here is my program as it stands now:
with open('C:\\datatext.txt', 'r') as datafile:
lines = datafile.read().split(' ')
matchedLines = [item for item in lines if item.startswith("nam1") or item.startswith("ndname")]
with (open('C:\\bigfile.txt', 'r'),
open('C:\\smallerdataset.txt', 'w')) as (completedataset, smallerdataset):
for line in completedataset:
for t in matchedLines:
if t in line:
smallerdataset.write(line)
completedataset.close()
smallerdataset.close()
How can I get around this hurdle?