4

Hello guys I'm looking for a solution to my code where I try to convert a CSV file into an XLSX file and all my data gets reduced into one column separated by ;. (see the pics below)

Could you please help me to solve one of the two codes in order to make the data representation when converting equal to the csv file?? (see pictures)

The two following codes give the same result: (important, I am using Python 3.6 env on Jupyter Notebook):


import os
import glob
import csv
from xlsxwriter.workbook import Workbook


for csvfile in glob.glob(os.path.join('.', 'LOGS.CSV')):
    workbook = Workbook(csvfile[:-4] + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'r') as f:
        reader = csv.reader((line.replace('\0','-') for line in f))
        for r, row in enumerate (reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)
    workbook.close()

import os
import csv
import sys

from openpyxl import Workbook

data_initial = open("new.csv", "r")
sys.getdefaultencoding()
workbook = Workbook()
worksheet = workbook.worksheets[0]
with data_initial as f:
    data = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",")
    for r, row in enumerate(data):
        for c, col in enumerate(row):
            for idx, val in enumerate(col.split('/')):
                cell = worksheet.cell(row=r+1, column=c+1)
                cell.value = val
workbook.save('output.xlsx')

This is my CSV file data organization: Picture:This is my CSV file data organization

And this is what I get when I convert it into an XLSX: Picture: And this is what I get when I convert it into an XLSX

Edit from comments

Okay, so I used @DeepSpace's program:

 import pandas as pd

 pd.read_csv('C:/Users/Pictures/LOGS.CSV')
   .to_excel('C:/Users/Pictures/excel.xlsx')

and I am still getting this: Image Program xlsx response

OKAY SOLUTION: The conversion is GREAT. But in my case the first column gets moved somehow. The Data num String is under nothing and the first column is its values... (see the pictures below)

CSV file

xlsx converted file

 import pandas as pd
    filepath_in = "C:/Users/Pictures/LOGS.csv"
    filepath_out = "C:/Users/Pictures/excel.xlsx"
    pd.read_csv(filepath_in, delimiter=";").to_excel(filepath_out)
icedwater
  • 4,701
  • 3
  • 35
  • 50
MarcoPolo11
  • 81
  • 1
  • 1
  • 11
  • 1
    If you don't mind the overhead of installing a new package, this whole logic can essentially be reduced to 1-2 lines of code: `import pandas as pd ; pd.read_csv('/path/to/csv').to_excel('/path/to/excel.xlsx')` – DeepSpace Feb 12 '18 at 13:54
  • It looks like the output delimiter is semicolon. Is there an argument to change that to comma? – Marichyasana Feb 12 '18 at 14:58
  • DeepSpace Thx for fast response and trying to help. But its still not working with your program, do you ahve any other ideas?? : import pandas as pd ; pd.read_csv('/path/to/csv').to_excel('/path/to/excel.xlsx') – MarcoPolo11 Feb 13 '18 at 08:49

1 Answers1

4

There were issues with your file. Rename or save them as .txt files first. Then as mentioned in comments, use pandas (@DeepSpace) and specify the delimiter (@Marichyasana).

Given

A renamed text file (e.g. LOGS1.txt) of semi-colon delimited columns, example:

0;2;DT#1970-01-01-00:46:09;55;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
1;2;DT#1970-01-01-00:46:25;71;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
2;2;DT#1970-01-01-00:46:28;74;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
3;2;DT#1970-01-01-00:46:30;76;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
4;2;DT#1970-01-01-00:46:32;78;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
5;2;DT#1970-01-01-00:46:34;80;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
...

Code

import pandas as pd


filepath_in = "C:/Users/Pictures/LOGS1.txt"
filepath_out = "C:/Users/Pictures/excel.xlsx"
pd.read_csv(filepath_in, delimiter=";").to_excel(filepath_out, index=False)

Apply the same process to the second file (LOGS2.txt).

Nate
  • 1,442
  • 14
  • 22
pylang
  • 40,867
  • 14
  • 129
  • 121
  • Thx for Helping, by the way My first Column gets moved and its just a white cell under my Title string do you know why?= (@pylang) – MarcoPolo11 Feb 13 '18 at 11:54
  • @MarcoPolo11 I'm not sure. I've tested it, and it should still would without [extra parameters](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html). Your csv seem odd. Can you post your csv as a plaint text file, i.e. open it in a *good* text editor and post the results. You should see plain text with semicolons between columns. – pylang Feb 13 '18 at 19:03
  • Done hope you are able to solve my problem. Thx for fast responding :) @pylang – MarcoPolo11 Feb 14 '18 at 07:20
  • Your files are not true csv files. Notice in excel, all of your data collects in the first column. You are better off renaming them `LOGS1.txt` and `LOGS2.txt`. See the update. – pylang Feb 14 '18 at 08:51
  • Cool @pylang thx for help its working. One last thing, do you know how to delete a whole column and specified column by me, in csv format or xlsx using python? – MarcoPolo11 Feb 15 '18 at 12:22
  • When you read in the file, it becomes a pandas DataFrame. If you search the net for pandas related questions, you'll find many answers from here forward, e.g. ["How to delete a column?"](https://stackoverflow.com/a/18145399/4531270), via `.drop(column)`. Then write it to `xslx`. – pylang Feb 15 '18 at 18:14