I have just over 2000 .txt files that I need to convert to .csv files. Each is sequentially labeled (i.e. nstar0001.txt, nstar0002.txt, etc...). I have searched multiple places for answers, but often the solutions are for Python2.x or use outdated libraries. Each star file has 7 columns of data that I want to label when converting to csv format.
Here is my most recent attempt:
import csv
import os
import itertools
##Convert all nstar####.txt files to csv
stars = int(input("Enter the TOTAL number of stars (including 'bad' stars):"))
k = 1
while k < stars + 1:
if k < 10:
q = 'nstar' + '0' + '0' + '0' + str(k) + '.txt'
r = 'nstar' + '0' + '0' + '0' + str(k) + '.csv'
with open(q, 'rb') as in_file:
stripped = (line.strip() for line in in_file)
lines = (line for line in stripped if line)
grouped = itertools.izip(*[lines] * 7)
with open(r, 'wb') as out_file:
writer = csv.write(out_file)
writer.writerow(('jd', 'mag', 'merr', 'id', 'cerr', 'serr', 'perr'))
writer.writerows(grouped)
This was borrowed from another StackOverflow question and slightly modified to suit my needs. However, upon running I get
AttributeError: module 'itertools' has no attribute 'izip'
I know this loop only works for the first few files, but just wanted to get it working before running it for all files.