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;








0















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.










share|improve this question






















  • 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

















0















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.










share|improve this question






















  • 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













0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 11:53









ElliottElliott

61




61












  • 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
















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












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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현