I'm struggling with self-taught programmer p. 130.

Asked 1 years ago, Updated 1 years ago, 388 views

import csv

with open("st.csv", "w", newline=') asf:
    w=csv.writer(f, delimiter=",")
    w.writerow ("one", "two", "three")
    w.writerow ("four", "five", "six")

I don't know if I should save this file as ".py" or ".csv".
Either way

import csv

with open("st.csv", "r") as f:
    r=csv.reader(f, delimiter=",")
    For row in r:
        print(", ".join(row)) 

When you run this program, you will see

on the shell.
one, two, three
four, five, six 

There is no result that

import csv

with open("st.csv", "w", newline=') asf:
    w=csv.writer(f, delimiter=",")
    w.writerow ("one", "two", "three")
    w.writerow ("four", "five", "six")

The result is that
The same thing happens when you open it in a text editor.
What should I do?

python python3

2022-10-31 00:00

2 Answers

You may miss the point because you don't have a self-taught programmer, but you can save the two scripts in one .py file in this order and run them.Save and run the following specifically:

import csv

with open("st.csv", "w", newline=') asf:
    w=csv.writer(f, delimiter=",")
    w.writerow ("one", "two", "three")
    w.writerow ("four", "five", "six")

with open("st.csv", "r") as f:
    r=csv.reader(f, delimiter=",")
    For row in r:
        print(", ".join(row))


2022-10-31 00:00

@Delft ViewYou must save it with the extension .py and run the program as shown in your answer.
(For example, save p130writecsv.py and run it like python p130writecsv.py)

This will result in a csv file called st.csv.

Running the second program, the first program appears because you save the first program under the name st.csv and then leave it there.

I have nothing to do with your question, but here are some articles in the same book that stumble for different reasons.
Exit CSV files in Python

Do you study by referring to the same book?Repository for stored programs (probably not the same as the book)
j-kato732/self_study_python
hama28/Python_Test


2022-10-31 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.