简体   繁体   English

python 中的文件“~”分隔文件中的问题

[英]issue in file "~" seperated file in python

I am a newbie in Python.我是 Python 的新手。 i have "~" separeted file in a folder for eg.我在文件夹中有“~”分隔文件,例如。

2021-11-02 23:04:17.106
#$!pre-dump!$#00067evnISO-8859-1    
#$!languages!$#
##$!cdf!$#
UAFR~UAFRICA~
UEUE~UEUR (CIS + TURKEY)~
UEUW~UEUR (WESTERN)~
UFEA~UASIA~
UGBR~UUNITED KINGDOM~
ULAM~ULATIN AMERICA~
UMEA~UMIDDLE EAST~
UNAM~UNORTH AMERICA~
UOCE~UOCEANIA~
UUSA~UUSA~

the files may have lines without "~".文件可能有没有“~”的行。 i need to get the lines which has "~" in a text file and here is the python script which i've done我需要在文本文件中获取具有“~”的行,这是我完成的 python 脚本

import os
sourcepath = os.listdir('input/')
for file in sourcepath:
    input_file = 'input/' + file
    print('conversion is going for:' + input_file)
    with open(input_file, 'r+', encoding='cp437') as input_file:
        input_file.seek(0)
        lines = input_file.readlines()
        input_file.seek(0)
        for line in lines:
            if "~" in line:
                input_file.write(line)

and below is the input file screenshot input file screenshot and here is the output which i got after running the above python script output of the python script and i really don't know where i am going wrong... please help me.. and below is the input file screenshot input file screenshot and here is the output which i got after running the above python script output of the python script and i really don't know where i am going wrong... please help me..

Rewriting a file in place doesn't delete extraneous data if you write less new data than the original file size.如果您写入的新数据少于原始文件大小,则就地重写文件不会删除无关数据。 But it's a trivial fix;但这是一个微不足道的修复; just explicitly truncate the file after you rewrite it, eg:只需在重写文件后显式truncate文件,例如:

import os

for file in os.listdir('input/'):
    input_file = 'input/' + file
    print('conversion is going for:' + input_file)
    with open(input_file, 'r+', encoding='cp437') as input_file:
        # Removed unnecessary initial seek; r+ defaults to beginning of file
        lines = input_file.readlines()
        input_file.seek(0)
        for line in lines:
            if "~" in line:
                input_file.write(line)
        input_file.truncate()  # With no arguments, truncate truncates to current file offset

Optionally, the truncate can be done after the .seek(0) instead, which empties the file before rewriting any of it (at the risk of losing all your data if something goes wrong writing out the new data, and possibly causing the new file to use new disk sectors instead of reusing already allocated space).或者,可以在.seek(0)之后进行truncate ,这会在重写任何文件之前清空文件(如果写出新数据出现问题,可能会丢失所有数据,并可能导致新文件使用新的磁盘扇区而不是重用已分配的空间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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