diff --git a/file_input.txt b/file_input.txt index ca8c2a8..3cbad6d 100644 --- a/file_input.txt +++ b/file_input.txt @@ -1,5 +1,7 @@ +# some comment "0261","+","Apple iPhone XR 128GB Red","https://www.apple.com/shop/buy-iphone/iphone-xr/6.1-inch-display-128gb-red-unlocked","12.5" "0092","+","Samsung Galaxy S21 5G 128GB Phantom Gray","https://www.samsung.com/us/smartphones/galaxy-s21-5g/buy/galaxy-s21-5g-128gb-unlocked-sm-g991uzkaxaa/","11.0" +# another comment "0035","+","Sony WH-1000XM4 Wireless Noise Cancelling Headphones","https://www.amazon.com/Sony-WH-1000XM4-Canceling-Headphones-Phone-Call/dp/B08CVRNPRZ/","4.3.0.0" "0054","+","Bose QuietComfort 35 II Wireless Bluetooth Headphones","https://www.amazon.com/Bose-QuietComfort-Wireless-Headphones-Cancelling/dp/B0756CYWWD/","4.5.2.0" "0169","+","Apple iPad Pro 11-inch Wi-Fi 256GB - Space Gray","https://www.apple.com/shop/buy-ipad/ipad-pro/11-inch-display-256gb-space-gray-wifi","14.5" diff --git a/file_output.txt b/file_output.txt index fe4b906..6b809a6 100644 --- a/file_output.txt +++ b/file_output.txt @@ -1,5 +1,7 @@ +# some comment "0001","+","Apple iPhone XR 128GB Red","https://www.apple.com/shop/buy-iphone/iphone-xr/6.1-inch-display-128gb-red-unlocked","12.5" "0002","+","Samsung Galaxy S21 5G 128GB Phantom Gray","https://www.samsung.com/us/smartphones/galaxy-s21-5g/buy/galaxy-s21-5g-128gb-unlocked-sm-g991uzkaxaa/","11.0" +# another comment "0003","+","Sony WH-1000XM4 Wireless Noise Cancelling Headphones","https://www.amazon.com/Sony-WH-1000XM4-Canceling-Headphones-Phone-Call/dp/B08CVRNPRZ/","4.3.0.0" "0004","+","Bose QuietComfort 35 II Wireless Bluetooth Headphones","https://www.amazon.com/Bose-QuietComfort-Wireless-Headphones-Cancelling/dp/B0756CYWWD/","4.5.2.0" -"0005","+","Apple iPad Pro 11-inch Wi-Fi 256GB - Space Gray","https://www.apple.com/shop/buy-ipad/ipad-pro/11-inch-display-256gb-space-gray-wifi","14.5" \ No newline at end of file +"0005","+","Apple iPad Pro 11-inch Wi-Fi 256GB - Space Gray","https://www.apple.com/shop/buy-ipad/ipad-pro/11-inch-display-256gb-space-gray-wifi","14.5" diff --git a/main.py b/main.py index 4ae49fc..55c6ba4 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,53 @@ # define custom opening and closing brackets for id id_opening_bracket = "\"" id_closing_bracket = "\"" +# define custom lines separator lines_separator = "," +# initialize line counter +i = 0 +# initialize output lines list +output_lines = [] + +############################## +# # +# open your input file # +# # +############################## with open('file_input.txt', 'r') as f: lines = f.readlines() -for i, line in enumerate(lines): - line = line.strip().split(lines_separator) # split line by comma - line[0] = id_opening_bracket + f"{i+1:04}" + id_closing_bracket # re-enumerate the first value - line = lines_separator.join(line) # join the modified values with comma - lines[i] = line +################################# +# # +# parse lines - main loop # +# # +################################# +for line in lines: + # process comments + if line.startswith("#"): + # add comment to output lines + # "rstrip()" removes the line break character (writes your comment without extra line break after it) + output_lines.append(line.rstrip()) + # process non-comment lines + else: + # split line by comma + line = line.strip().split(lines_separator) + # re-enumerate the first value + line[0] = id_opening_bracket + f"{i+1:04}" + id_closing_bracket + # join the modified values with comma + line = lines_separator.join(line) + # add modified line to output lines + output_lines.append(line) + i += 1 + + +######################################## +# # +# write data to your output file # +# # +######################################## with open('file_output.txt', 'w') as f: - f.write('\n'.join(lines)) \ No newline at end of file + # add line break at the end of the file + f.write('\n'.join(output_lines) + '\n') + \ No newline at end of file