Skip to content

Commit 5f155fd

Browse files
author
Kostya K
committed
CSV Exporter: Handle the situation when a user's name contains a special char. Issue #19.
1 parent 01efdd0 commit 5f155fd

File tree

1 file changed

+12
-7
lines changed
  • telegram_messages_dump/exporters

1 file changed

+12
-7
lines changed

telegram_messages_dump/exporters/csv.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ def format(self, msg, exporter_context):
4141
# msg.date.hour, msg.date.minute, msg.id, "RE_ID=%s " % re_id if re_id else "",
4242
# name, self._py_encode_basestring(content))
4343

44-
# Check if name contains ','. If it does surround the name with quotes.
45-
if name and name.find(",") != -1:
46-
if name.startswith("\"") and name.endswith("\""):
47-
name = '"' + name + '"'
44+
name, isNameModified = self._py_encode_basestring(name)
45+
46+
# Check if name contains ',' or there were other special chars.
47+
# If it does surround the name with quotes.
48+
if name and (name.find(",") != -1 or isNameModified):
49+
name = '"' + name + '"'
4850

4951
msg_dump_str = ",".join([str(msg.id),
5052
msg.date.isoformat(),
5153
name,
5254
str(re_id),
53-
'"' + str(self._py_encode_basestring(content)) + '"'])
55+
'"' + str(self._py_encode_basestring(content)[0]) + '"'])
5456
return msg_dump_str
5557

5658
def begin_final_file(self, resulting_file, exporter_context):
@@ -65,7 +67,10 @@ def begin_final_file(self, resulting_file, exporter_context):
6567
def _py_encode_basestring(self, s):
6668
"""Return a JSON representation of a Python string"""
6769
if not s:
68-
return s
70+
return s, False
71+
isAnyCharReplaced = False
6972
def replace(match):
73+
nonlocal isAnyCharReplaced
74+
isAnyCharReplaced = True
7075
return self.ESCAPE_DICT[match.group(0)]
71-
return self.ESCAPE.sub(replace, s)
76+
return str(self.ESCAPE.sub(replace, s)), isAnyCharReplaced

0 commit comments

Comments
 (0)