def open_json_or_csv_somehow(filename, date_format=None):
fileformat = None<EOL>if filename.endswith('<STR_LIT>'):<EOL><INDENT>fileformat = '<STR_LIT>'<EOL><DEDENT>elif filename.endswith('<STR_LIT>'):<EOL><INDENT>fileformat = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>with open(filename) as opened:<EOL><INDENT>line = opened.readline()<EOL>if line[<NUM_LIT:0>] not in '<STR_LIT>' and not filename.endswith('<STR_LIT>'):<EOL><INDENT>fileformat = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if (line.count('<STR_LIT:{>') == line.count('<STR_LIT:}>') and<EOL>line.count('<STR_LIT:[>') == line.count('<STR_LIT:]>')):<EOL><INDENT>char = '<STR_LIT:U+0020>'<EOL>while char.isspace():<EOL><INDENT>char = opened.read()<EOL>if char == '<STR_LIT>':<EOL><INDENT>fileformat = '<STR_LIT>'<EOL>break<EOL><DEDENT><DEDENT>if fileformat is None:<EOL><INDENT>fileformat = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fileformat = '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if fileformat == '<STR_LIT>':<EOL><INDENT>stream = json.load(open(filename), encoding='<STR_LIT:utf-8>')<EOL><DEDENT>elif fileformat == '<STR_LIT>':<EOL><INDENT>stream = open_csv_somehow(filename)<EOL><DEDENT>else:<EOL><INDENT>stream = stream_json_lines(filename)<EOL><DEDENT>return _normalize_data(stream, date_format=date_format)<EOL>
Deduce the format of a file, within reason.

- If the filename ends with .csv or .txt, it's csv.
- If the filename ends with .jsons, it's a JSON stream (conveniently the
  format we want to output).
- If the filename ends with .json, it could be a legitimate JSON file, or
  it could be a JSON stream, following a nonstandard convention that many
  people including us are guilty of. In that case:
  - If the first line is a complete JSON document, and there is more in the
    file besides the first line, then it is a JSON stream.
  - Otherwise, it is probably really JSON.
- If the filename does not end with .json, .jsons, or .csv, we have to guess
  whether it's still CSV or tab-separated values or something like that.
  If it's JSON, the first character would almost certainly have to be a
  bracket or a brace. If it isn't, assume it's CSV or similar.