1

I'm a beginner in python coding I wrote a little script to convert text files in a folder to csv files. The script is running but it delivers a csv file with only one column. Where's my fault? Also the converted files contain always .txt.csv. I would appreciate some help. Thanks a lot!

Thats the format of the txt. file

Thats my code

import os
import pandas as pd


input = "C:\\path where original data is"
output = "C:\\path for csv files"
os.chdir(input)

txt_files = os.listdir('.')
print(txt_files)

for txt_file in (txt_files):
    df = pd.read_csv(txt_file, delimiter = ',',  names=['Messpunkt\t','Zeichnungspunkt   \t','Eigenschaft\t','Position\t', 'Sollmaß\t','Toleranz\t','Abweichung\t','Lage\t' ])
    df.to_csv(output+txt_file+'.csv', index=False)
 
joho
  • 35
  • 6

1 Answers1

0

The problem could be the delimiter, try changing it to '\t' since from the picture you have posted it seems like the tabulation is the actual delimiter, another possible approach could be to change the read function to pd.read_table

FrancescoPenasa
  • 220
  • 4
  • 12
  • 1
    Thanks for quick reply. I already tried different delimters, none of them worked (including \t). I also tried sep \n as replacement of delimiter. I tried also pd.read_table but that did also not work. – joho Sep 09 '21 at 13:08
  • Another possible try it could be '\s+', have you checked the content of df? – FrancescoPenasa Sep 09 '21 at 13:27
  • 1
    Thanks, sep \s+ worked. The only problem now is, by saving the file it creates every time a .txt.csv File. – joho Sep 10 '21 at 07:56
  • For that we could strip the string `output+txt_file+'.csv` and for example replace the substring '.txt' in txt_file something like txt_file.replaceAll('.txt', '') – FrancescoPenasa Sep 10 '21 at 08:01