-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|