如何在 python 中迭代文本文件中的行?

How to iterate lines from a text file in python?

我更新了我的问题,因为起初我认为我有最好的解决方案但是,直到 now.It 是我在执行中的错误,我才找到它。我认为错误来自文件 C 中的循环。

我正在尝试从文本文件中读取行"Plaintext.txt"。

e0370734313198a2885a308d3243f6a8
ccddeeff8899aabb4455667700112233
8e73b0f7da0e6452c810f32bc4567a22

它现在包含两行,我只放了两行以便进行简单测试,但我必须放超过 1000 条文本(意味着超过 1000 行)我想读取每一行然后将其发送到 uart我会对每个明文进行加密(加密算法在C中): 这是我的脚本:

我按你说的编辑了,但我还有一行加密

    import string
import serial
import time
from array import array
import struct
import binascii

ser = serial.Serial(
                    port='COM4',\
                    baudrate=230400,\
                    parity=serial.PARITY_NONE,\
                    stopbits=serial.STOPBITS_ONE,\
                    bytesize=serial.EIGHTBITS,\
                    timeout=0)  

f = open(r'C:\Users\user\Plaintxt.txt', 'r')
for a in f:
   plaintxt_16b=a[0:32]
   plaintext=binascii.unhexlify(plaintxt_16b)
   clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext

ser.write(clear_msg)
time.sleep(0.4)

while True: 
  print(ser.read(70))
ser.close()                # close ports

在 C 文件中:

   while(1)
    {
        int rx_length = dev_uart_ptr->uart_read((void*)rx_buffer, 19);

        if (rx_length <19)
        {

            if (rx_buffer[0]=='\x24')
            {
                if (rx_buffer[1]=='\x73')
                {
                    if (rx_buffer[2]=='\x10')
                    {
                        plaintext[0] = (rx_buffer[3] << 24)  |
                                (rx_buffer[4] << 16)  |
                                (rx_buffer[5] << 8)   |
                                rx_buffer[6];
                        plaintext[1] = (rx_buffer[7] << 24)  |
                                (rx_buffer[8] << 16)  |
                                (rx_buffer[9] << 8)   |
                                rx_buffer[10];
                        plaintext[2] = (rx_buffer[11] << 24) |
                                (rx_buffer[12] << 16) |
                                (rx_buffer[13] << 8)  |
                                rx_buffer[14];
                        plaintext[3] = (rx_buffer[15] << 24) |
                                (rx_buffer[16] << 16) |
                                (rx_buffer[17] << 8)  |
                                rx_buffer[18];

                        xprintf("**************************\n");
                        xprintf("%8x %8x %8x %8x \n",plaintext[0],plaintext[1],plaintext[2],plaintext[3]);
                        aes2_set_msg((unsigned int *)plaintext);    /** Reset AES message buffer */
                        aes2_set_key128((unsigned int *)key128);    /** Put the key 128 into AES */
                        /** Configure  AES register  to enable IRQ and ENCODE */
                        regs_aes2_ptr-> CFG =  AES2_CFG_ENC_DEC_BIT | AES2_CFG_IRQ_MASK_BIT;
                        /** Reset AES internaly */
                        regs_aes2_ptr-> CTRL = AES2_CTRL_SWRESET_BIT;

#if DEBUG
                        xprintf("Go encrypt..\n");
#endif
                        /** Start the ENCODE function */
                        regs_aes2_ptr-> CTRL = AES2_CTRL_START_BIT;

                        while(!aes2_irq_flag); /** Wait for irq flag */
                        aes2_irq_flag=0;    /** Reset irq flag */

#if DEBUG
                        xprintf("Encrypt done..\n");
#endif

                        aes2_get_msg((unsigned int *)ciphertext);   /** Retrieve encrypted message */
                        xprintf("%8x %8x %8x %8x \n",ciphertext[0],ciphertext[1],ciphertext[2],ciphertext[3]);
                        xprintf("**************************\n");

                    }
                    else
                    {
                        printf ("false");
                    }

                }
                else
                {
                    printf ("false");
                }
            }

        }

    }// End While

}//end of C_Entry

所以问题是它只需要最后一行并一直重复该行的相同加密:

    $**************************
ccddeeff 8899aabb 44556677   112233
Go encrypt..
Encrypt do
ne..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
**************************
******************
********
ccddeeff 8899aabb 44556677   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d
8d055 c5c7573a 8df4e9aa
**************************
**************************
ccddeeff
 8899aabb 44556677   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df
4e9aa
**************************
**************************
ccddeeff 8899aabb 44556677
   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
**********
****************
**************************
ccddeeff 8899aabb 44556677   112233
Go enc
rypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
....................

如果你能帮助我,我将不胜感激。

您可能需要执行以下操作:

f = open("your_file", "r")
for line in f:
    do_something(line)
f.close()

或评论指出:

with open("your_file", "r") as f:
    for line in f:
        do_something(line)

Python 将遍历每一行,并在此处将行的字符串作为变量行。您可以用这种方式处理文件中的每一行。另外,这样做,python每次读取一行,所以对大文件有效。

在你的 for 循环中

for a in range (0,2):

   line_array=lines[a]
   plaintxt_16b=line_array[0:32]

   plaintext=binascii.unhexlify(plaintxt_16b)
   clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext

你在每次迭代中覆盖变量 clear_msg。 离开循环后,它包含最后一个值。在您的情况下,最后一行(加密)。然后你多次发送变量 clear_msg 而不改变它的内容。 您还需要缩进以下块:

print(clear_msg)
ser.write(clear_msg)
time.sleep(0.4)

根据 this,调用 readlines() 会使您的代码变慢、不那么明确、不那么简洁,完全没有任何好处。

无需关闭文件即可打开文件的一个好方法是使用 while

with open(file) as f:
    line = f.read().splitlines()

line 是一个包含文件所有行的列表。您可以像在列表中那样迭代并获取任何条目

问题是 python 文件中的空格:for 循环必须包含

       ser.write(clear_msg)
       time.sleep(0.4)
       print(ser.read(70))

这样就不会只从明文文件中取出最后一个明文。

import string
import serial
import time
from array import array
import struct
import binascii

ser = serial.Serial(
                    port='COM4',\
                    baudrate=230400,\
                    parity=serial.PARITY_NONE,\
                    stopbits=serial.STOPBITS_ONE,\
                    bytesize=serial.EIGHTBITS,\
                    timeout=0)  

f = open(r'C:\Users\user\Plaintxt.txt', 'r')
for a in f:
   plaintxt_16b=a[0:32]
   plaintext=binascii.unhexlify(plaintxt_16b)
   clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext
   ser.write(clear_msg)
   time.sleep(0.4)
   print(ser.read(70))
ser.close()                # close ports

我的问题已经解决了。非常感谢您的帮助。