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

标签 python c uart

我更新我的问题是因为起初我认为我有最好的解决方案但是,直到现在我还没有。这是我在执行时的错误。我认为错误来自文件 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每次读取一行,所以对大文件有效。

关于python - 如何在 python 中迭代文本文件中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40886685/

相关文章:

Python PDB 只进入单个文件?

c - seq_file 接口(interface)中是否有与 C 的 setbuf 等效的功能?

c - 我需要使用stm uart模块

emulation - 虚拟电调POS打印机

c - ATMEGA 2560 uart 代码未在 minicom 上给出正确的输出

python - 你如何让 djangorestframework 使用格式后缀返回 xml?

python - 如何生成与标度无关的随机 float ?

python - Tumblr API 发布到辅助博客

C 函数数组

c - Lex 文件无法识别关键字。如何编写正则表达式来读取特定关键字?