Reputation: 789
I have a program which let the user input som data which then get drawn on the canvas (like a weekly scheme). I now want the user to be able to delete or redo an entry (which i will do by a click event), however, my question goes:
How do i read a specific line in my textfile? A sample of a possible textfile:
Abe
#0080c0
February
Friday
21
Part delivery
John
#ff8000
July
Sunday
21
Social
Egon
#ff80ff
April
Thursday
15
Work
Note that this is a small part of the textfile, ideally it would contain 56 of theese 6-line-clusters.
my code for inserting the text:
taskentry = str(tasknameentry.get())
monthentry = str(monthvar.get())
dayentry = str(dayvar.get())
dateentry = str(datevar.get())
activityentry = str(typevar.get())
tasklist = [taskentry, hexstr, monthentry, dayentry, dateentry, activityentry]
taskfile = open('taskfile.txt', 'a')
for word in tasklist:
taskfile.write('%s \n' % word)
taskfile.write('\n')
taskfile.close()
So for the user to be able to redo or delete a "task" i need to read the file and look for a specific task name or something similar, im just not sure how to. I've read through the .write and .read part of my book, looked at the docs and questions here but havent been able to produce a valid solution.
What i need now, so i can move on is just a way to read fx the 6 appending lines when searching for fx John. So i in the future can have a redo or delete event choosen by a click.
In advance, thanks!
Best regards, Casper
Upvotes: 1
Views: 267
Reputation: 8726
If the file is not that big, you can read the file into a buffer, modify that buffer and write the buffer back to the file. A quick code snippet would be like:
#open the file
f = open('file.txt')
lines = f.readlines()
lineNum = -1
#find the line to modify
for i, line in enumerate(lines):
if line.strip() == "John":
lineNum = i
break
if lineNum == -1:
#Line not found, handle the error..
#modify the buffer with the new data
newtasklist = [taskentry, hexstr, monthentry, dayentry, dateentry, activityentry]
for task in newtasklist:
lines[lineNum] = task
lineNum += 1
#or if you want to remove the task list :
lines = lines[:lineNum] + [lineNum + 7:]
# and write everything back
with open('file.txt', 'w') as file:
file.writelines(lines)
Upvotes: 1
Reputation: 5939
You can make the list in which each entry is a separate line of your txt file by using readlines. Then you can alter particular enries in this list and use writelines to change the txt file.
Tasks=open('taskfile.txt', 'r').readlines()
In data you have tasks of each person by name:
Data={}
n=0
try:
while True:
Data[Tasks[n+n*6]]=Tasks[n+1:n+6]
n+=1
except:
pass
Then you can change the second task of John by:
Data['John'][1]='Football'
Upvotes: 0
Reputation: 19
First of all, i'm not the best at this, but if you scan your entire document and store it into an ArrayList of Strings, you would then be able to delete certain lines, add lines, and modify lines.
After doing that, you can erase your document's information and repaste whats in the ArrayList.
As for reading a specific line, i don't actually think there is a way to read the line using Scanner or FileWriter.
Sorry if this doesn't really help. I am doing my best to help in any way!
Some Example Code Might be:
ArrayList<String> taskList = new <String>ArrayList;
File file = new File(<File Name>);
try
{
Scanner scan = new Scanner(file);
while(scan.hasNext())
{
taskList.add(scan.next());
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
return;
}
This will create a searchable array of the words which you can then delete and add to as much as you want, then clear your document and re-paste the new information from the array list in it.
Upvotes: 0