Skip to content

Commit

Permalink
comments parsing added
Browse files Browse the repository at this point in the history
  • Loading branch information
a13xe committed Apr 13, 2023
1 parent dc29514 commit 241af99
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions file_input.txt
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 3 additions & 1 deletion file_output.txt
Original file line number Diff line number Diff line change
@@ -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"
"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"
49 changes: 43 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -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))
# add line break at the end of the file
f.write('\n'.join(output_lines) + '\n')

0 comments on commit 241af99

Please sign in to comment.