patternpythonMinor
Manipulating CSV files to regular text file
Viewed 0 times
filemanipulatingcsvtextregularfiles
Problem
At my job, often I have to manually convert *.csv Files structured like:
To a regular *.txt file with the structure:
This file is an input to another program, and rather than spending the time to copy and paste the excel sheet into notepad and crop out all the commas and make sure there are spaces and such I decided to write a python script that would do it for me.
Now, to be fair, I am new to Python, and this script works perfectly (first try), I would just like some feedback on my methods, or ways to improve my quality of code.
number,number\n
number,number\n
number,number\n
...To a regular *.txt file with the structure:
#1
double filename(number of columns, number of columns)
number number
number number
number number
...This file is an input to another program, and rather than spending the time to copy and paste the excel sheet into notepad and crop out all the commas and make sure there are spaces and such I decided to write a python script that would do it for me.
Now, to be fair, I am new to Python, and this script works perfectly (first try), I would just like some feedback on my methods, or ways to improve my quality of code.
# A simple program to create a formatted text file from a *.csv file.
csv_file = input('Enter the name of your input file: ')
txt_file = input('Enter the name of your output file: ')
try:
my_input_file = open(csv_file, "r")
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
if not my_input_file.closed:
text_list = [];
for line in my_input_file.readlines():
line = line.split(",", 2)
text_list.append(" ".join(line))
my_input_file.close()
try:
my_output_file = open(txt_file, "w")
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
if not my_output_file.closed:
my_output_file.write("#1\n")
my_output_file.write("double({},{})\n".format(len(text_list), 2))
for line in text_list:
my_output_file.write(" " + line)
print('File Successfully written.')
my_output_file.close()Solution
Use
There are great benefits discussed in the docs. Summarizing it handles closing and exceptions automatically. It makes it possible to write shorter code: look Andrey's answer for a rewrite.
Miscellaneous
:
with when dealing with files.There are great benefits discussed in the docs. Summarizing it handles closing and exceptions automatically. It makes it possible to write shorter code: look Andrey's answer for a rewrite.
Miscellaneous
- Drop my from the start of your variables' names.
- Learn list comprehension because it is extremely powerful, here I give you an example:
:
with open(csv_file) as input_file:
lines = [line.split(",", 2) for line in input_file.readlines()]
text_list = [" ".join(line) for line in lines]Code Snippets
with open(csv_file) as input_file:
lines = [line.split(",", 2) for line in input_file.readlines()]
text_list = [" ".join(line) for line in lines]Context
StackExchange Code Review Q#81990, answer score: 3
Revisions (0)
No revisions yet.