Code adds line again instead of deletes it from file - Python Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!using Python for deleting a specific line in a fileHow do I copy a file in Python?How to get file creation & modification date/times in Python?How do I remove/delete a folder that is not empty with Python?How do I create a Java string from the contents of a file?Python join: why is it string.join(list) instead of list.join(string)?How to read a file line-by-line into a list?How do you append to a file in Python?Delete an element from a dictionaryDelete a file or folderWhy is reading lines from stdin much slower in C++ than Python?
What does 丫 mean? 丫是什么意思?
A term for a woman complaining about things/begging in a cute/childish way
What is the difference between CTSS and ITS?
How to ternary Plot3D a function
Why is a lens darker than other ones when applying the same settings?
Can you force honesty by using the Speak with Dead and Zone of Truth spells together?
Monty Hall Problem-Probability Paradox
What are the main differences between Stargate SG-1 cuts?
My mentor says to set image to Fine instead of RAW — how is this different from JPG?
How many time has Arya actually used Needle?
Putting class ranking in CV, but against dept guidelines
Sally's older brother
How to change the tick of the color bar legend to black
What does Turing mean by this statement?
What is the difference between a "ranged attack" and a "ranged weapon attack"?
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
Trying to understand entropy as a novice in thermodynamics
Does the Mueller report show a conspiracy between Russia and the Trump Campaign?
Delete free apps from library
How can I save and copy a screenhot at the same time?
Can two people see the same photon?
I can't produce songs
Universal covering space of the real projective line?
The test team as an enemy of development? And how can this be avoided?
Code adds line again instead of deletes it from file - Python
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!using Python for deleting a specific line in a fileHow do I copy a file in Python?How to get file creation & modification date/times in Python?How do I remove/delete a folder that is not empty with Python?How do I create a Java string from the contents of a file?Python join: why is it string.join(list) instead of list.join(string)?How to read a file line-by-line into a list?How do you append to a file in Python?Delete an element from a dictionaryDelete a file or folderWhy is reading lines from stdin much slower in C++ than Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Regardless of what I do to this code, it will always duplicate the line in the file instead of deleting it from the file.
I had planned to single out the part of the code I want to remove. Then rewrite the file using all data other than what was selected out in the 'part' variable.
Below is my add function to show hows its added to the CSV file, and the remove function which is broken.
def add_employee(): #THIS WORKS
EmployeeID = int(input("Please enter the employee ID here "))
EmployeeName = input("Please enter the name here ")
EmployeeDep = input("Please enter the department here ")
EmployeeDes = input("Please enter the designation here ")
EmployeeStartDate = input("Please enter the start date here in format of DD-MM-YYYY ")
EmployeeStatus = input("Please enter the status here : Active/Deactive ")
with open('database.csv', "a") as f:
employee_add = ""
employee_add += "%s," % EmployeeID
employee_add += "%s," % EmployeeName
employee_add += "%s," % EmployeeDep
employee_add += "%s," % EmployeeDes
employee_add += "%s," % EmployeeStartDate
employee_add += "%s," % EmployeeStatus
employee_add += "n"
f.write(employee_add)
f.close()
print("Added correctly")
continue_or_quit()
def remove_employee():
id_to_delete = input("Enter the ID of the employee you would like to deleten")
with open('database.csv', "r+") as f:
for line in f:
for part in line.split():
if id_to_delete in part:
print("nice")
for part in line.split():
if part != id_to_delete +"n":
f.write(part)
I have looked at using Python for deleting a specific line in a file however, the code didn't seem to work for my situation.
python file csv
add a comment |
Regardless of what I do to this code, it will always duplicate the line in the file instead of deleting it from the file.
I had planned to single out the part of the code I want to remove. Then rewrite the file using all data other than what was selected out in the 'part' variable.
Below is my add function to show hows its added to the CSV file, and the remove function which is broken.
def add_employee(): #THIS WORKS
EmployeeID = int(input("Please enter the employee ID here "))
EmployeeName = input("Please enter the name here ")
EmployeeDep = input("Please enter the department here ")
EmployeeDes = input("Please enter the designation here ")
EmployeeStartDate = input("Please enter the start date here in format of DD-MM-YYYY ")
EmployeeStatus = input("Please enter the status here : Active/Deactive ")
with open('database.csv', "a") as f:
employee_add = ""
employee_add += "%s," % EmployeeID
employee_add += "%s," % EmployeeName
employee_add += "%s," % EmployeeDep
employee_add += "%s," % EmployeeDes
employee_add += "%s," % EmployeeStartDate
employee_add += "%s," % EmployeeStatus
employee_add += "n"
f.write(employee_add)
f.close()
print("Added correctly")
continue_or_quit()
def remove_employee():
id_to_delete = input("Enter the ID of the employee you would like to deleten")
with open('database.csv', "r+") as f:
for line in f:
for part in line.split():
if id_to_delete in part:
print("nice")
for part in line.split():
if part != id_to_delete +"n":
f.write(part)
I have looked at using Python for deleting a specific line in a file however, the code didn't seem to work for my situation.
python file csv
The default separator forsplit()
is the white space, which means that yourpart
values will contain the,
separator you added in your CSV. I think that could be a part of your problem.
– Maxouille
Mar 22 at 12:33
add a comment |
Regardless of what I do to this code, it will always duplicate the line in the file instead of deleting it from the file.
I had planned to single out the part of the code I want to remove. Then rewrite the file using all data other than what was selected out in the 'part' variable.
Below is my add function to show hows its added to the CSV file, and the remove function which is broken.
def add_employee(): #THIS WORKS
EmployeeID = int(input("Please enter the employee ID here "))
EmployeeName = input("Please enter the name here ")
EmployeeDep = input("Please enter the department here ")
EmployeeDes = input("Please enter the designation here ")
EmployeeStartDate = input("Please enter the start date here in format of DD-MM-YYYY ")
EmployeeStatus = input("Please enter the status here : Active/Deactive ")
with open('database.csv', "a") as f:
employee_add = ""
employee_add += "%s," % EmployeeID
employee_add += "%s," % EmployeeName
employee_add += "%s," % EmployeeDep
employee_add += "%s," % EmployeeDes
employee_add += "%s," % EmployeeStartDate
employee_add += "%s," % EmployeeStatus
employee_add += "n"
f.write(employee_add)
f.close()
print("Added correctly")
continue_or_quit()
def remove_employee():
id_to_delete = input("Enter the ID of the employee you would like to deleten")
with open('database.csv', "r+") as f:
for line in f:
for part in line.split():
if id_to_delete in part:
print("nice")
for part in line.split():
if part != id_to_delete +"n":
f.write(part)
I have looked at using Python for deleting a specific line in a file however, the code didn't seem to work for my situation.
python file csv
Regardless of what I do to this code, it will always duplicate the line in the file instead of deleting it from the file.
I had planned to single out the part of the code I want to remove. Then rewrite the file using all data other than what was selected out in the 'part' variable.
Below is my add function to show hows its added to the CSV file, and the remove function which is broken.
def add_employee(): #THIS WORKS
EmployeeID = int(input("Please enter the employee ID here "))
EmployeeName = input("Please enter the name here ")
EmployeeDep = input("Please enter the department here ")
EmployeeDes = input("Please enter the designation here ")
EmployeeStartDate = input("Please enter the start date here in format of DD-MM-YYYY ")
EmployeeStatus = input("Please enter the status here : Active/Deactive ")
with open('database.csv', "a") as f:
employee_add = ""
employee_add += "%s," % EmployeeID
employee_add += "%s," % EmployeeName
employee_add += "%s," % EmployeeDep
employee_add += "%s," % EmployeeDes
employee_add += "%s," % EmployeeStartDate
employee_add += "%s," % EmployeeStatus
employee_add += "n"
f.write(employee_add)
f.close()
print("Added correctly")
continue_or_quit()
def remove_employee():
id_to_delete = input("Enter the ID of the employee you would like to deleten")
with open('database.csv', "r+") as f:
for line in f:
for part in line.split():
if id_to_delete in part:
print("nice")
for part in line.split():
if part != id_to_delete +"n":
f.write(part)
I have looked at using Python for deleting a specific line in a file however, the code didn't seem to work for my situation.
python file csv
python file csv
asked Mar 22 at 11:53
ElliottElliott
61
61
The default separator forsplit()
is the white space, which means that yourpart
values will contain the,
separator you added in your CSV. I think that could be a part of your problem.
– Maxouille
Mar 22 at 12:33
add a comment |
The default separator forsplit()
is the white space, which means that yourpart
values will contain the,
separator you added in your CSV. I think that could be a part of your problem.
– Maxouille
Mar 22 at 12:33
The default separator for
split()
is the white space, which means that your part
values will contain the ,
separator you added in your CSV. I think that could be a part of your problem.– Maxouille
Mar 22 at 12:33
The default separator for
split()
is the white space, which means that your part
values will contain the ,
separator you added in your CSV. I think that could be a part of your problem.– Maxouille
Mar 22 at 12:33
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55299041%2fcode-adds-line-again-instead-of-deletes-it-from-file-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55299041%2fcode-adds-line-again-instead-of-deletes-it-from-file-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The default separator for
split()
is the white space, which means that yourpart
values will contain the,
separator you added in your CSV. I think that could be a part of your problem.– Maxouille
Mar 22 at 12:33