0

I have tried several of the suggested ways to do this on Stack Overflow and none have worked so far. I have a few differently formatted CSV files that I want imported into a Excel workbook and to add a new workbook with some formulas written in.

here are the links of what I have tried: Link1 Link2 Link3

I have tried what they suggested and nothing happens in the best case and in the worst I get a Null error. FileName1: YYYY-MM-DD.csv format1:

2019/09/04 00:00  32.85 17.94 21.04 0.00
2019/09/04 00:00  32.98 15.77 21.43 -0.02
2019/09/04 00:00  32.85 15.23 21.21 -0.09
2019/09/04 00:01  32.93 15.51 21.30 0.06
2019/09/04 00:01  32.96 15.54 21.45 0.36
2019/09/04 00:01  33.09 17.49 21.26 0.02
2019/09/04 00:01  34.74 17.34 21.17 0.00
2019/09/04 00:02  35.08 17.87 20.62 -0.06

FileName2:YYYY-MM-DD.csv Formate2:

2019/05/01 09:36  30.8,67.6
2019/05/01 09:37  28.8,57.6
2019/05/01 09:38  27.2,53.6
2019/05/01 09:39  27.3,53.4
2019/05/01 09:40  27.0,50.5
2019/05/01 09:41  27.8,54.8
2019/05/01 09:42  25.7,47.1
2019/05/01 09:43  25.8,49.3
2019/05/01 09:44  25.8,48.4
2019/05/01 09:45  26.0,50.0

FileName3:YYYYMMDD.csv Formate3:

2019/09/16 08:00 ,001896100BE3,26C,79RH,2,4,8
2019/09/16 08:01 ,001896100BE3,27C,77RH,2,4,7
2019/09/16 08:01 ,001896100BE3,27C,75RH,3,5,5
2019/09/16 08:02 ,001896100BE3,28C,74RH,2,4,5
6
  • 2
    Can you provide an example of a csv file? Commented Oct 4, 2019 at 17:05
  • Differently formatted you mean different separators and decimals for each file? Do they have a pattern in name -> format? Commented Oct 4, 2019 at 17:06
  • Too many questions. Is first file fixed width? tab delimited? space delimited? do you want date/time in one field or two? Commented Oct 4, 2019 at 19:29
  • Second file looks like 3 fields (if date/time one field), tab or space delimiter then a comma between two numeric fields Commented Oct 4, 2019 at 19:30
  • Third file would work as a comma delimited - next questions would be put 'em on multiple sheets? Basically, what do you need to do with the data? Commented Oct 4, 2019 at 19:31

1 Answer 1

1

A basic approach:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from contextlib import closing
from csv import reader
from xlsxwriter import Workbook

if __name__ == '__main__':

    # file2

    # open workbook
    workbook = Workbook('file2.xlsx')

    # add worksheet
    page1 = workbook.add_worksheet()

    with closing(open('file2.csv', 'r')) as csv_file:
        # read the csv file
        reader_orig = reader(csv_file, delimiter=' ')

        # iterate through the rows
        row = 0
        for el in reader_orig:
            res = dict()
            res['date'] = el[0]
            res['time'] = el[1]
            res['c1'] = el[3].split(',')[0]
            res['c2'] = el[3].split(',')[1]

            # fill in the result of reading to xlsx
            col = 0
            for item in res:
                page1.write(row, col, res[item])
                col += 1

            row += 1

    workbook.close()

data in excel

You can see other cases here.

Sign up to request clarification or add additional context in comments.

6 Comments

I will have to try this on Monday, I don't have access to the files over the weekend
tried running the example you had for the first file and got this error:``` for el in reader_orig: _csv.Error: line contains NULL byte ```
It looks like you have another encoding, not UTF8, or something like that. Try to play with it, like stackoverflow.com/questions/7894856/…
Both utf-16 and utf-16le I get: with closing(open('2019-09-04.csv', 'r', 'utf-16le')) as csv_file: TypeError: an integer is required (got type str)
Which one OS do you use? Can u send this file via an email to [email protected]?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.