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

6
  • 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 stackoverflow.com/q/3348460/4046632 Commented Sep 9, 2021 at 7:32
  • @buran okay the blank line is solved now, but the string/list story not. Commented Sep 9, 2021 at 7:36
  • Did you check the link in my first comment? Commented Sep 9, 2021 at 7:36
  • Yes, but could not solve it. Commented Sep 9, 2021 at 7: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) Commented Sep 9, 2021 at 7:47

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.