简体   繁体   中英

How to write bytes to a file?

I'm porting some Python 2.7 code to 3.10 and I'm having trouble with Bytes vs String.

I have added.encode("utf-8") to all the.startswith and.endswith

The problem I have is at the end, where I get the error

...
 destination.write("{}\n".format(delimiter.join(line_tokens)))
TypeError: sequence item 0: expected str instance, bytes found

How to correct this? Here is my method:

def fix_csv_file(csv_file_path, delimiter="\t"):
  temp_file_path = "{}.temp".format(csv_file_path)
  with open(csv_file_path, "rb") as source:
    with open(temp_file_path, "wb") as destination:
      for line in source:
        # Remove carriage return and new line characters.
        if line.endswith("\r\n".encode("utf-8")):
          line = line[:-2]
        elif line.endswith("\n".encode("utf-8")):
          line = line[:-1]
        # Clean up columns.
        line_tokens = line.split(delimiter.encode("utf-8"))
        for idx, token in enumerate(line_tokens):
          token = token.strip()
          if token == "(null)" or token == "\"(null)\"":
            token = "\"\""
          else:
            if not token.startswith("\"".encode("utf-8")) and \
               not token.endswith("\"".encode("utf-8")):
              token = "\"{}\"".format(token)
          line_tokens[idx] = token
        destination.write("{}\n".format(delimiter.join(line_tokens)))
  os.remove(csv_file_path)
  os.rename(temp_file_path, csv_file_path)

This should fix it.

def fix_csv_file(csv_file_path, delimiter="\t"):
    temp_file_path = "{}.temp".format(csv_file_path)
    with open(csv_file_path, "rt") as source:
        with open(temp_file_path, "rt") as destination:
            for line in source:
                if line.endswith("\r\n"):
                    line = line[:-2]
                elif line.endswith("\n"):
                    line = line[:-1]
                line_tokens = line.split(delimeter)
                for idx, token in enumerate(line_tokens):
                    token = token.strip()
                    if token == "(null)" or token == "\"(null)\"":
                        token = "\"\""
                    else:
                        if not token.startswith("\"") \ 
                            and not token.endswith("\""):
                            token = "\"{}\"".format(token)
                    line_tokens[idx] = token
                 destination.write("{}\n".format(delimiter.join(line_tokens)))
    os.remove(csv_file_path)
    os.rename(temp_file_path, csv_file_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM