Reputation: 850
I am trying to split up a tab delimited byte object in to lines and fields. In my input data when a field is supposed to be empty the data has -- . I want to replace -- with something that will act as a empty when I use it to build a mysql insert ''. I am new to list comprehension but I found a few examples that seemed similar.
for line in line_split[1:]:
field_split=line.split(b'\t')
field_split = [x.replace('--', '') for x in field_split]
print("f-", field_split)
report_list.append(field_split)
If I comment out the replace line that errors so it can print, I get back the following line. If you scroll right the field value i want to replace shows b'--'. This seems like it should be a simple fix but I have messing around for way longer then I care to admit
f- [b'1020569383', b'X012312', b'42132LVPG0U', b'Glow', b'Sports', b'Glow', b'Amazon', b'18.85', b'18.85', b'11.61', b'10.67', b'1.54', b'36.02', b'inches', b'0.52', b'pounds', b'Lg-Std-Non-Media', b'USD', b'6.02', b'2.83', b'0.00', b'--', b'--', b'--', b'3.19']
Upvotes: 0
Views: 105
Reputation: 3669
I can suggest you two options. First is to remove it from the dict-
x = [b'1020569383', b'X012312', b'42132LVPG0U', b'Glow', b'Sports', b'Glow', b'Amazon', b'18.85', b'18.85', b'11.61', b'10.67', b'1.54', b'36.02', b'inches', b'0.52', b'pounds', b'Lg-Std-Non-Media', b'USD', b'6.02', b'2.83', b'0.00', b'--', b'--', b'--', b'3.19']
final_list = [i for i in x if i != b"--"]
Or replace it with ''
-
final_list = [i.replace(b"--", b"") for i in x]
What's that b
in front of these texts? If you do type(x[0])
you get <class 'bytes'>
. So you need to perform your operations in byte
string also, hence the b""
Upvotes: 1