Skip to content

Instantly share code, notes, and snippets.

@wiljdaws
Created August 14, 2023 19:06
Show Gist options
  • Save wiljdaws/2c439c02eac421246776a773ab50cff6 to your computer and use it in GitHub Desktop.
Save wiljdaws/2c439c02eac421246776a773ab50cff6 to your computer and use it in GitHub Desktop.
Convert tab delimited file to csv good for dwp ETL txt exports.
import re
import os
import shutil
desktop_path = os.path.expanduser("~/Desktop")
downloads_path = os.path.expanduser("~/Downlaods")
# get username
username = os.getlogin()
def convert_to_csv(txt_file):
'''
This function converts a txt file to a csv file.\n
- The txt file is located in one of the following: \n
- Desktop \n
- Downloads \n
- Desktop\ Text Files \n
1) The csv file is saved on the desktop.
2) The txt file is moved to a folder called Text Files.
3) The csv file is moved to a folder called csv Files.
Parameters:
txt_file (str): The name of the txt file.
Returns:
None
Example Usage::
>>> convert_to_csv('test.txt')
Raises:
FileNotFoundError: If the txt file is not found in desktop, in Text Files, or in Downloads.
'''
# if txt file is not found raise error
if not os.path.exists(desktop_path + f'/{txt_file}'):
# look for txt file in Text Files
print(f'{txt_file} not found on desktop')
print(f'{username}, I will begin checking the Text Files...')
if os.path.exists(desktop_path + f'/Text Files/{txt_file}'):
file_path = desktop_path + f'/Text Files'
print(f'{txt_file} found in Text Files')
else:
print(f'{txt_file} not found in Text Files')
print(f'{username}, I will begin checking Downloads...')
if os.path.exists(downloads_path + f'{txt_file}'):
file_path = downloads_path
print(f'{txt_file} found in Downlaods')
file_path = downloads_path
else:
# raise FileNotFoundError
raise FileNotFoundError(f'{txt_file} not found on desktop, in Text Files, or in Downloads')
else:
file_path = desktop_path
print(f'{txt_file} found on desktop')
file_name = txt_file.split('.')[0]
with open(file_path + f'/{txt_file}', 'r') as infile, open(file_path + f'/{file_name}.csv', 'w') as outfile:
for line in infile:
line = re.sub(r',', ';', line)
line = re.sub(r'\t', ',', line, 7)
outfile.write(line)
# move the txt file to a folder called txt_files
if not os.path.exists(desktop_path + '/Text Files'):
os.makedirs(desktop_path + '/Text Files')
# if the file is not already in Text Files
if file_path != desktop_path + '/Text Files':
shutil.move(file_path + f'/{txt_file}', desktop_path + '/Text Files')
if not os.path.exists(desktop_path + '/CSV Files'):
os.makedirs(desktop_path + '/CSV Files')
# if the csv does not already exist in CSV Files
if not os.path.exists(desktop_path + f'/CSV Files/{file_name}.csv'):
shutil.move(file_path + f'/{file_name}.csv', desktop_path + '/CSV Files')
print(f'{file_name}.csv has been created and moved to CSV Files')
if __name__ == '__main__':
convert_to_csv('damage_land.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment