#πŸ”’ Problem with writing csv file

23 messages Β· Page 1 of 1 (latest)

unique lynx
#

it works fine without time, but i want to add time to it, when i added time, its dont writing my data, and its just file without any extensions like .csv
code:

# Write csv file
def write_csv(data: list) -> None:
    # Date in YEAR-MONTH-DAY-HOUR:MINUTE format
    t = datetime.now().strftime("%Y-%m-%d-%H:%M")

    name = f"data_{t}.csv"

    with open(name, "w", newline="", encoding="utf-8") as f:
        w = csv.writer(f)
        w.writerow(["PRICE", "OLD PRICE", "NAME", "LINK"])
        # writing the data rows
        w.writerows(data)
hasty hingeBOT
#

@unique lynx

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

zenith jackal
unique lynx
zenith jackal
#

So it creates the file with the correct file name but without the .csv extension and that file has no data

unique lynx
#

yeah

zenith jackal
#

Ur using csv and datetime right?

unique lynx
#

right

zenith jackal
#

Alright one second

unique lynx
zenith jackal
#
import csv
from datetime import datetime

def write_csv(data: list) -> None:
    t = datetime.now().strftime("%Y-%m-%d-%H%M")
    name = f"data_{t}.csv"
    with open(name, "w", newline="", encoding="utf-8") as f:
        w = csv.writer(f)
        w.writerow(["PRICE", "OLD PRICE", "NAME", "LINK"])
        w.writerows(data)

# Example data: list of lists
data = [
    ["test", "test", "test", "test"]
]

write_csv(data)
unique lynx
#

oh tysm, this works, but i dont understand isnt it the same?

zenith jackal
#

your format was messing it up

#

because of the ":"

unique lynx
#

oh

zenith jackal
#

its an invalid character for a file name

unique lynx
#

i didnt knew, thanks

faint raven
#

(It's invalid on Windows.)

zenith jackal
#

yeah took me a bit to spot it too no worries

unique lynx
#

!close

hasty hingeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.