2

Im using csv to write rows to an empty text file, but when I open the textfile I see there is a space between each letter/character. I have no idea what I'm doing wrong.

This is my code:

import csv
import configparser

# Read local `config.ini` file.
config = configparser.ConfigParser()                                     
config.read(r'C:\data\FF\Desktop\Studio\cfg.ini')

header_1= config['HEADERS']['headers_1']
header_2 =config['HEADERS']['headers_2']

full_path = r'C:\data\FF\Desktop\Studio\New Text Document.txt' 

with open(full_path, 'w') as output:
    writer = csv.writer(output, delimiter = '\t')
    writer.writerow(header_1)
    writer.writerow(header_2)

This is how cfg.ini looks like:

[HEADERS]
headers_2 = ['VEHICLE', 'MODEL', 'DSG', 'YEAR', 'MONTH', 'DAY', 'HOUR', 'MINUTE','SECOND']

headers_1 = ['*****data*****']

This is how New Text Document.txt looks like: enter image description here

Mediterráneo
  • 115
  • 1
  • 9
  • 1
    The problem is not the `csv.writerow`, but how you read the list from `cfg.ini`. You read string, not list (as you think). And to fix the extra blank line - check https://stackoverflow.com/q/3348460/4046632 – buran Sep 09 '21 at 07:32
  • @buran okay the blank line is solved now, but the string/list story not. – Mediterráneo Sep 09 '21 at 07:36
  • Did you check the link in my first comment? – buran Sep 09 '21 at 07:36
  • Yes, but could not solve it. – Mediterráneo Sep 09 '21 at 07:45
  • import `json`, then `header_1 = json.loads(config['HEADERS']['headers_1'])`. That is assuming you don't change the current format in `cfg.ini`, only replace `'` with `"` (double quotes, not 2 single-quotes) – buran Sep 09 '21 at 07:47
  • Thank you. If you can place that in an answer I can accept your answer. – Mediterráneo Sep 09 '21 at 07:52

1 Answers1

0

The problem is not the csv.writerow, but how you read the list from cfg.ini. You read string, not list (as you think). Check Lists in ConfigParser And to fix the extra blank line (when using csv module on windows) you need to specify newline='' - check CSV file written with Python has blank lines between each row

cfg.ini

[HEADERS]
headers_2 = ["VEHICLE", "MODEL", "DSG", "YEAR", "MONTH", "DAY", "HOUR", "MINUTE","SECOND"]
headers_1 = ["*****data*****"]

your file:

import csv
import configparser
import json

# Read local `config.ini` file.
config = configparser.ConfigParser()                                     
config.read(r'C:\data\FF\Desktop\Studio\cfg.ini')

header_1 = json.loads(config['HEADERS']['headers_1'])
header_2 = json.loads(config['HEADERS']['headers_2'])

full_path = r'C:\data\FF\Desktop\Studio\New Text Document.txt' 

with open(full_path, 'w', newline='') as output:
    writer = csv.writer(output, delimiter = '\t')
    writer.writerow(header_1)
    writer.writerow(header_2)
buran
  • 13,682
  • 10
  • 36
  • 61
  • your question should be closed as [double] duplicate, not upvoted. I post the answer, only to avoid any misunderstanding from my advise in the comments. – buran Sep 09 '21 at 07:56
  • @buran I think he is right, the question you tagged is not that clear. We also need to respect the way mediterraneo asked his question, very clear in what is he looking for. – NorthAfrican Sep 09 '21 at 08:01
  • Could not agree more with @NorthAfrican, this question is very clear and we see exactly what he wants. Deserves nothing but respect and an upvote obviously. (despite a bit of duplication with your link) Lot of developers can learn from the way this question is asked. – Premier12 Sep 09 '21 at 08:04