简体   繁体   中英

how to properly read and modify a file using python

I'm trying to remove all (non-space) whitespace characters from a file and replace all spaces with commas. Here is my current code:

def file_get_contents(filename):
  with open(filename) as f:
    return f.read()

content = file_get_contents('file.txt')
content = content.split
content = str(content).replace(' ',',')
with open ('file.txt', 'w') as f:
  f.write(content)

when this is run, it replaces the contents of the file with:

<built-in,method,split,of,str,object,at,0x100894200>

The main issue you have is that you're assigning the method content.split to content, rather than calling it and assigning its return value. If you print out content after that assignment, it will be: <built-in method split of str object at 0x100894200> which is not what you want. Fix it by adding parentheses, to make it a call of the method, rather than just a reference to it:

content = content.split()

I think you might still have an issue after fixing that through. str.split returns a list, which you're then tuning back into a string using str (before trying to substitute commas for spaces). That's going to give you square brackets and quotation marks, which you probably don't want, and you'll get a bunch of extra commas. Instead, I suggest using the str.join method like this:

content = ",".join(content) # joins all members of the list with commas

I'm not exactly sure if this is what you want though. Using split is going to replace all the newlines in the file, so you're going to end up with a single line with many, many words separated by commas.

When you split the content, you forgot to call the function. Also once you split, its an array so you should loop to replace things.

def file_get_contents(filename):
  with open(filename) as f:
    return f.read()

content = file_get_contents('file.txt')
content = content.split() <- HERE
content = [c.replace(' ',',') for c in content]
content = "".join(content)
with open ('file.txt', 'w') as f:
  f.write(content)

if you are looking to replace characters i think you would be better off using python's re module for regular expressions. sample code would be as follows:

import re

def file_get_contents(filename):
  with open(filename) as f:
    return f.read()

if __name__=='__main__':
    content = file_get_contents('file.txt')
    # First replace any spaces with commas, then remove any other whitespace
    new_content = re.sub('\s', '', re.sub(' ', ',', content))
    with open ('new_file.txt', 'w') as f:
      f.write(new_content)

its more succinct then trying to split all the time and gives you a little bit more flexibility. just also be careful with how large of a file you are opening and reading with your code - you may want to consider using a line iterator or something instead of reading all the file contents at once

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