Skip to content

Commit

Permalink
more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Cube707 committed Jul 12, 2022
1 parent bb9563e commit 62f8fc3
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions csv2xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,49 @@ def main(glob_pattern, outdir, separator):

spinner = cycle(["-", "/", "|", "\\"]) # a spinner to dislay "work in progress"

# loop over all files matching the glob pattern
for csvfile in glob(glob_pattern):

# handle output dir:
outdir = path.join(path.dirname(csvfile), outdir)
makedirs(outdir, exist_ok=True) # ensure outdir exists

# create workbook object:
xlsxfile = path.join(outdir, path.basename(csvfile)[:-4] + ".xlsx")
workbook = Workbook(xlsxfile)
worksheet = workbook.add_worksheet()
r, c = 0, -1
with open(csvfile, "rt", encoding="utf8") as fp:

r = 0
c = -1 # we start at c=-1 because the first action is to increment c to 0
with open(csvfile, "r") as fp:

for line in fp.readlines():
# remove newline characters from the end of the line:
line = line.strip("\n")

# loop over all the elements in a line:
for token in line.split(separator):
c += 1
token = token.strip(" \t")
c += 1 # first thing we do is increment so we can skip empty tokens
token = token.strip(" \t") # remove uneeded whitespace
if not token:
continue
continue # skip empty tokens

# escaped "string" tokens are written as is:
if token.startswith('"'):
token = token.strip('"')
worksheet.write(r, c, token)

# all not-strings are assumed to be numbers:
else:
if token == "nan":
continue
continue # skip nan's
try:
worksheet.write_number(r, c, float(token))
except ValueError: # Non-numbers that are not escaped with "
worksheet.write(r, c, token)
c = -1
r += 1
print("\r" + next(spinner), end="")
c = -1
print("\r" + next(spinner), end="") # display "work in progress"
try:
workbook.close()
print(f"\rconverted: {csvfile} -> {xlsxfile}")
Expand Down

0 comments on commit 62f8fc3

Please sign in to comment.