Python Forum
Number stored as text with openpyxl
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number stored as text with openpyxl
#3
(Mar-19-2024, 08:31 PM)deanhystad Wrote: csv.reader returns a list of strings. You need to do any type conversion yourself. I don't think the csv library is a good choice for what you are doing. I don't think openpyxl is a good choice either. I would use pandas. It can read a csv file and do the type conversion, probably automatically, and it can write the results to a spreadsheet.

As a poor alternative you could fix the types here:
                for row in reader:
                    row[5] = int(row[5])  # Changing column 5 to an int.
                    ws.append(row)
                    wb.save(xlsx_file)  # Should not be in loop
You could convert values in row from str to int/float/date/whatever before appending to the worksheet.

The file you treat as a CSV file is not a CSV file. It stops being a CSV file when you do this:
write_file.write(f'\nErrors: {error_count}\n')
CSV files must be tabular. All rows in a CSV file must have the same number of values.

I forgot to reply to this, sorry, but I took your advice and I'm using pandas now, which is much better. Thank you!
Reply


Messages In This Thread
Number stored as text with openpyxl - by CAD79 - Mar-19-2024, 12:30 PM
RE: Number stored as text with openpyxl - by CAD79 - Apr-17-2024, 10:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to perform DESC table sort on dates stored as TEXT type. hammer 7 2,309 Mar-15-2022, 01:10 PM
Last Post: hammer
  Search text in PDF and output its page number. atomxkai 21 9,304 Jan-21-2022, 06:20 AM
Last Post: snippsat
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,214 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  Split Column Text by Number of Characters cgoldstein 3 3,065 Mar-11-2019, 01:45 PM
Last Post: perfringo
  get the number in the line in text file lateublegende 2 2,557 Jan-29-2019, 06:03 PM
Last Post: lateublegende
  get number of unread emails + email text Pedroski55 3 4,362 Oct-04-2018, 12:33 PM
Last Post: Larz60+
  Find number in a text for if statement BitbyBit 3 3,343 Jul-13-2018, 04:38 PM
Last Post: BitbyBit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020