0

I have a csv file called doctors.csv containing

1,John,Smith,Internal Med
2,Jone,Smith,Pediatrics
3,George,Carlos,Cardiology

I have code that is going to replace just the first name of one row. It asks for which row they would like to replace and then asks for what they want to replace it with

file = open("doctors.csv", "r")
x= 0
tmplist = []
for row in file:
    tmplist.append(row)
file.close
for row in tmplist:
    print (x,row)
    x = x+1
rowchange = int(input("Enter the row number to change: "))
makechange= input(str("What is the new first name: "))
file.close()

with open("doctors.csv","r") as doctorcsv:
    r = csv.reader(doctorcsv) 
    lines = list(r)
    print (lines)
    lines[rowchange][1]=makechange
    print(lines)

writer = csv.writer(open('doctors.csv', 'w'))
writer.writerows(lines)

here's what I have but it introduces newlines into the csv file which would cause issues for other parts of my system like this.

1,John,Smith,Internal Med

2,Jone,Smith,Pediatrics

3,George,Carlos,Cardiology

How can I delete the newlines in the csv file?

KDecker
  • 6,928
  • 8
  • 40
  • 81
G0rg81
  • 5
  • 2
  • @Axe319 thanks this helped solve my problem but I can't mark your comment as a solution :). – G0rg81 Dec 05 '22 at 17:31
  • Ther are a few duplicates, so I'm hesitant to provide an answer. [CSV file written with Python has blank lines between each row](https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row) Here's one of those questions. – Axe319 Dec 05 '22 at 17:33

0 Answers0