Python Forum
How to sort .csv file test log which item first fail and paint color
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sort .csv file test log which item first fail and paint color
#21
def myApp():
    # save all .csv as .xlsx
    import pandas as pd
    import glob

    savepath = '/home/pedro/myPython/pandas/'
    files = glob.glob(savepath + '*.csv')

    for csv in files:
        print(csv)
        df = pd.read_csv(csv_file)
        name = csv.split('.')        
        df.to_excel(name[0] + '.xlsx', index=False)
Reply
#22
(Sep-01-2022, 07:49 AM)Pedroski55 Wrote:
def myApp():
    # save all .csv as .xlsx
    import pandas as pd
    import glob

    savepath = '/home/pedro/myPython/pandas/'
    files = glob.glob(savepath + '*.csv')

    for csv in files:
        print(csv)
        df = pd.read_csv(csv_file)
        name = csv.split('.')        
        df.to_excel(name[0] + '.xlsx', index=False)

Thank you so much for your help. But used this converted to xlsx seems got error format. Actually I have try other way converted it as this wrong format yet, so still try csv library and pandas. You can refer to attached.
Reply
#23
There was a mistake in line 11

Quote:>>> myApp()
/home/pedro/myPython/pandas/csv_files/poll_count20BECW.csv
Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "<pyshell#2>", line 1, in <module>
File "<pyshell#1>", line 11, in myApp
NameError: name 'csv_file' is not defined

Line 11 should be:

df = pd.read_csv(csv)

This very quickly converted 3 csv files to Excel.

def myApp():
    # save all .csv as .xlsx
    import pandas as pd
    import glob

    savepath = '/home/pedro/myPython/pandas/csv_files/'
    files = glob.glob(savepath + '*.csv')

    for csv in files:
        print(csv)
        df = pd.read_csv(csv)
        name = csv.split('.')        
        df.to_excel(name[0] + '.xlsx', index=False)
Output:
>>> myApp() /home/pedro/myPython/pandas/csv_files/poll_count20BECW.csv /home/pedro/myPython/pandas/csv_files/Log.csv /home/pedro/myPython/pandas/csv_files/poll_count20BECW (1).csv >>>
I often do this the other way round: use pandas to create an Excel, then use pandas to convert that to csv.

Then I can import the csv into MySQL.
Reply
#24
import glob
import os
import csv
import xlsxwriter

files = glob.glob(r'C:\Users\Sam\OneDrive\Desktop\python_demo\*.csv')
workbook = xlsxwriter.Workbook(r'C:\Users\Sam\OneDrive\Desktop\python_demo\Log.xlsx')

row_numer = 0

for file_path in files:
    file = open(file_path)
    csvreader = csv.reader(file)

    file_name = os.path.basename(file_path)
    file_no_ext = os.path.splitext(file_name)[0]
    worksheet1 = workbook.add_worksheet(file_no_ext)
    row_numer = 0
    for row in csvreader:
        for index in range(len(row)):
            worksheet1.write(row_numer, index, row[index])
        row_numer += 1
    file.close()
workbook.close()









import csv
import openpyxl

def csv_to_excel(csv_filename, excel_filename):
    # Read CSV file
    csv_data = []
    with open(csv_filename) as f:
        csv_data = [row for row in csv.reader(f)]

    # Write to Excel file
    workbook = openpyxl.workbook.Workbook()
    worksheet = workbook.active
    for row in csv_data:
        worksheet.append(row)
    workbook.save(excel_filename)


if __name__ == "__main__":
    csv_to_excel("Log.csv", "Log.xlsx")
Tried two ways to converted from .csv to 。xlsx.content format is string,but save as .xlsx by manual format keep it.
Reply
#25
Looks good!

Everything OK now??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 983 Oct-25-2023, 09:09 AM
Last Post: codelab
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 614 Oct-18-2023, 11:09 PM
Last Post: Edward_
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 952 Feb-15-2023, 05:34 PM
Last Post: zsousa
  How to calculated how many fail in each site(s) in csv files SamLiu 4 1,279 Sep-26-2022, 06:28 AM
Last Post: SamLiu
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,307 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,583 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Remove an item from a list contained in another item in python CompleteNewb 19 5,661 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,449 Aug-22-2021, 06:59 PM
Last Post: Winfried
  scraping video src fail jacklee26 5 3,496 Jul-11-2021, 09:38 AM
Last Post: snippsat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,109 Jul-03-2021, 10:07 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

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