Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csv: Doesn't append a new row with writerow() #100821

Closed
LucasColas opened this issue Jan 7, 2023 · 3 comments
Closed

csv: Doesn't append a new row with writerow() #100821

LucasColas opened this issue Jan 7, 2023 · 3 comments
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error

Comments

@LucasColas
Copy link

LucasColas commented Jan 7, 2023

Bug report

When I want to add a new row to an existing file it doesn't append a new row. But it extends the last row.

def ajout_csv(nom, ordre, lignes):
  with open(nom + '.csv', mode='a', newline='') as csv_file:
      writer = csv.DictWriter(csv_file, fieldnames=ordre)
      #writer.writeheader()
      #writer = csv.writer(csv_file)
      for ligne in lignes:
        print("ligne",ligne)
        writer.writerow(ligne)
      csv_file.close()

ajout_csv('diabetes_UCI', 
          ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome'],
          
          [{'Pregnancies': '42', 'Glucose': '73', 'BloodPressure': '50', 'SkinThickness': '10', 'Insulin': '0', 'BMI': '23', 'DiabetesPedigreeFunction': '0.248', 'Age': '21', 'Outcome': '0'}])

Results :

{'Pregnancies': '1', 'Glucose': '93', 'BloodPressure': '70', 'SkinThickness': '31', 'Insulin': '0', 'BMI': '30.4', 'DiabetesPedigreeFunction': '0.315', 'Age': '23', 'Outcome': '042', None: ['73', '50', '10', '0', '23', '0.248', '21', '0']}
@LucasColas LucasColas added the type-bug An unexpected behavior, bug, or error label Jan 7, 2023
@AlexWaygood AlexWaygood changed the title Doesn't append a new row with writerow() csv: Doesn't append a new row with writerow() Jan 7, 2023
@LucasColas
Copy link
Author

Ok it seems it's the first append that doesn't append a new row. But if I do a second append it works.

@sobolevn
Copy link
Member

sobolevn commented Jan 7, 2023

A couple of comments:

  1. I've executed your code, it works as expected. The first time I get one row 42,73,50,10,0,23,0.248,21,0, the second time I have two rows (they are obviously the same). Not sure what the bug is.
  2. You don't need to csv_file.close(), it will be handled by with open() automatically. That the whole point of it

Going to mark this as pending to be closed, because of 1.

@sobolevn sobolevn added the pending The issue will be closed if no feedback is provided label Jan 7, 2023
@akulakov
Copy link
Contributor

akulakov commented Jan 8, 2023

It seems like Lucas is expecting that csv module will examine the file and if it's non-empty and doesn't end with a newline, prefix the row with a newline. That's not unreasonable but csv docs do not promise that, and it would be a backwards incompatible change, and it's very easy to add a newline explicitly if needed, so I agree this should be closed, and I don't think docs need to clarify this scenario.

@sobolevn sobolevn closed this as completed Jan 8, 2023
@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants