简体   繁体   English

反转 Python 中文本文件每行的最后 4 个字符 3

[英]Reverse the 4 last characters from each line of a text file in Python 3

I am trying to move the last 4 characters from each end of a line of a text file at the front of the line.我正在尝试从行首的文本文件的每一行的每一端移动最后 4 个字符。

For instance I have the following line: 6c6d6e6f707172737475767778797a7b10050例如我有以下行:6c6d6e6f707172737475767778797a7b10050

and I want the last 4 digits to get in the front of the line so I have the following code to do that:我希望最后 4 位数字位于该行的前面,所以我有以下代码来做到这一点:

import sys
import re
import os

def rearrange(word):
 if word.endswith('0'):         
     word = word[-4] + word[-3] + word[-2] + word[-1] + word[:-4]    
 print(word)
 return word

rearrange('6c6d6e6f707172737475767778797a7b10050')

I get the following output, hence so far so good, it works:我得到以下 output,因此到目前为止一切顺利,它可以工作:

00506c6d6e6f707172737475767778797a7b1 00506c6d6e6f707172737475767778797a7b1

However, when I try to do it for a small file with an iteration loop.但是,当我尝试对带有迭代循环的小文件执行此操作时。 Say the text file to read from is with the following lines:假设要读取的文本文件包含以下几行:

6c6d6e6f707172737475767778797a7b10050
8d6d6e6f707172737475767778797a7b10020
9a6d6e6f707172737475767778797a7b10030

I want to work the same way but it I cannot get it to work.我想以同样的方式工作,但我无法让它工作。 There is something obvious that is happening that I cannot see it, can someone spot it and help me out here please?有一些明显的事情正在发生,我看不到它,有人可以发现它并在这里帮助我吗?

import sys
import re
import os

def rearrange(word):
     if word.endswith('0'):         
         word = word[-4] + word[-3] + word[-2] + word[-1] + word[:-4]    
     print("print while the routine is executed:")
     print(word)
     return word     

file_write = open (r"reversed-test_text1.txt",'wt')

with open(r"test_text1.txt") as file_read:
    for line in file_read:        
        r = rearrange(str(line))
        file_write.write(r)
     
file_read.close()
file_write.close()

f = open("reversed-test_text1.txt", 'r')
print('Print the 4-char reversed text:')   
print(f.read())

When I execute it I get the following which is not what I wanted:当我执行它时,我得到以下不是我想要的:

Print the 4-char reversed text:
6c6d6e6f707172737475767778797a7b10050
8d6d6e6f707172737475767778797a7b10020
9a6d6e6f707172737475767778797a7b10030

Does anyone have any idea why?有谁知道为什么? It must be dead simple but I fail to see it (not that experienced in Python either).它一定很简单,但我看不到它(在 Python 中也没有经历过)。

I think the problem is, that by reading the lines from the file, you have a \n in the end of each line.我认为问题在于,通过从文件中读取行,每行末尾都有一个\n By your check for the zero in the rearrange -method, you are simply skipping your rearranging.通过检查rearrange方法中的零,您只是跳过了重新排列。

The if word.endswith('0'): condition is not satisfied, because each line ends with a newline character ( '\n' ). if word.endswith('0'):条件不满足,因为每一行都以换行符( '\n' )结尾。

To make it work as you expect, you might rewrite the loop as follows:要使其按预期工作,您可以按如下方式重写循环:

with open(r"test_text1.txt") as file_read:
    for line in file_read:        
        r = rearrange(line.rstrip())
        file_write.write(r + os.linesep)

The .rstrip() method trims the whitespaces characters from the end of the string. .rstrip()方法从字符串末尾修剪空白字符。

The os.linesep is the default line separator of the current platform. os.linesep是当前平台的默认行分隔符。

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

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