Skip to content

Commit c721fe5

Browse files
Fix end of line character issue.
1 parent c4d3edb commit c721fe5

4 files changed

Lines changed: 20 additions & 18 deletions

File tree

6.56 KB
Binary file not shown.
7.12 KB
Binary file not shown.

ip2location_csv_converter/ip2location_csv_converter.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ def range_number_to_ip(row, write_mode):
3939
if (write_mode == 'replace'):
4040
# new_row = '"' + from_ip + '","' + to_ip + '","' + remaining_columns
4141
if remaining_columns == '':
42-
new_row = '"' + from_ip + '","' + to_ip + '"\r\n'
42+
new_row = '"' + from_ip + '","' + to_ip + "\"\n"
4343
else:
44-
new_row = '"' + from_ip + '","' + to_ip + '","' + remaining_columns + '\r\n'
44+
new_row = '"' + from_ip + '","' + to_ip + '","' + remaining_columns + "\n"
4545
# print (new_row)
4646
elif (write_mode == 'append'):
4747
# new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + '","' + remaining_columns
4848
if remaining_columns == '':
49-
new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + '"\r\n'
49+
new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + "\"\n"
5050
else:
51-
new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + '","' + remaining_columns + '\r\n'
51+
new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + '","' + remaining_columns + "\n"
5252
return new_row
5353

5454
def number_to_cidr(row, write_mode):
@@ -72,15 +72,15 @@ def number_to_cidr(row, write_mode):
7272
remaining_columns += row[i] + '","'
7373
if (write_mode == 'replace'):
7474
if remaining_columns == '':
75-
new_row = '"' + ar1[0] + '"\r\n'
75+
new_row = '"' + ar1[0] + "\"\n"
7676
else:
77-
new_row = '"' + ar1[0] + '","' + remaining_columns + '\r\n'
77+
new_row = '"' + ar1[0] + '","' + remaining_columns + "\n"
7878
# print (new_row)
7979
elif (write_mode == 'append'):
8080
if remaining_columns == '':
81-
new_row = '"' + row[0] + '","' + row[1] + '","' + ar1[0] + '"\r\n'
81+
new_row = '"' + row[0] + '","' + row[1] + '","' + ar1[0] + "\"\n"
8282
else:
83-
new_row = '"' + row[0] + '","' + row[1] + '","' + ar1[0] + '","' + remaining_columns + '\r\n'
83+
new_row = '"' + row[0] + '","' + row[1] + '","' + ar1[0] + '","' + remaining_columns + "\n"
8484
# print (new_row)
8585
except:
8686
print ("Skipped invalid (range) data record")
@@ -110,27 +110,27 @@ def number_to_hex(row, write_mode, conversion_mode):
110110
# new_row = '"' + from_ip + '","' + to_ip + '","' + remaining_columns
111111
if sys.version < '3':
112112
if remaining_columns == '':
113-
new_row = '"' + from_hex + '","' + to_hex + '"\r\n'
113+
new_row = '"' + from_hex + '","' + to_hex + '"'
114114
else:
115-
new_row = '"' + from_hex + '","' + to_hex + '","' + remaining_columns + '\r\n'
115+
new_row = '"' + from_hex + '","' + to_hex + '","' + remaining_columns + "\n"
116116
else:
117117
if remaining_columns == '':
118-
new_row = '"' + str(from_hex) + '","' + str(to_hex) + '"\r\n'
118+
new_row = '"' + str(from_hex) + '","' + str(to_hex) + "\"\n"
119119
else:
120-
new_row = '"' + str(from_hex) + '","' + str(to_hex) + '","' + remaining_columns + '\r\n'
120+
new_row = '"' + str(from_hex) + '","' + str(to_hex) + '","' + remaining_columns + "\n"
121121
# print (new_row)
122122
elif (write_mode == 'append'):
123123
# new_row = '"' + row[0] + '","' + row[1] + '","' + from_ip + '","' + to_ip + '","' + remaining_columns
124124
if sys.version < '3':
125125
if remaining_columns == '':
126-
new_row = '"' + row[0] + '","' + row[1] + '","' + from_hex + '","' + to_hex + '"\r\n'
126+
new_row = '"' + row[0] + '","' + row[1] + '","' + from_hex + '","' + to_hex + "\"\n"
127127
else:
128-
new_row = '"' + row[0] + '","' + row[1] + '","' + from_hex + '","' + to_hex + '","' + remaining_columns + '\r\n'
128+
new_row = '"' + row[0] + '","' + row[1] + '","' + from_hex + '","' + to_hex + '","' + remaining_columns + "\n"
129129
else:
130130
if remaining_columns == '':
131-
new_row = '"' + row[0] + '","' + row[1] + '","' + str(from_hex) + '","' + str(to_hex) + '"\r\n'
131+
new_row = '"' + row[0] + '","' + row[1] + '","' + str(from_hex) + '","' + str(to_hex) + "\"\n"
132132
else:
133-
new_row = '"' + row[0] + '","' + row[1] + '","' + str(from_hex) + '","' + str(to_hex) + '","' + remaining_columns + '\r\n'
133+
new_row = '"' + row[0] + '","' + row[1] + '","' + str(from_hex) + '","' + str(to_hex) + '","' + remaining_columns + "\n"
134134
return new_row
135135

136136
def convert_to_csv(input_file, output_file, conversion_mode, write_mode):
@@ -160,10 +160,12 @@ def convert_to_csv(input_file, output_file, conversion_mode, write_mode):
160160
if sys.version < '3':
161161
my_list.append(new_row.decode('utf-8'))
162162
else:
163+
# detect_eol(new_row)
164+
print(repr(new_row))
163165
my_list.append(new_row)
164166

165167
if (len(my_list) > 0):
166-
with open(output_file, 'a') as myWrite:
168+
with open(output_file, 'a', newline='\r\n') as myWrite:
167169
myWrite.write(''.join(my_list))
168170

169171
my_list = []

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setuptools.setup(
1212
name="ip2location-python-csv-converter",
13-
version="1.2.2",
13+
version="1.2.3",
1414
description="Python script to converts IP2Location CSV database into IP range or CIDR format.",
1515
long_description_content_type="text/markdown",
1616
long_description=long_description,

0 commit comments

Comments
 (0)