久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何同時遍歷兩個文件中的行?

How to iterate across lines in two files simultaneously?(如何同時遍歷兩個文件中的行?)
本文介紹了如何同時遍歷兩個文件中的行?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有兩個文件,我想對它們執(zhí)行一些逐行操作.(換句話說,每個文件的第一行是對應(yīng)的,第二行也是如此,等等.)現(xiàn)在,我可以想到一些稍微麻煩的方法來同時遍歷兩個文件.然而,這是 Python,所以我想有一些語法簡寫.

I have two files, and I want to perform some line-wise operation across both of them. (In other words, the first lines of each file correspond, as do the second, etc.) Now, I can think of a number of slightly cumbersome ways to iterate across both files simultaneously; however, this is Python, so I imagine that there is some syntactic shorthand.

換句話說,有沒有一些簡單的方法來適應(yīng)

In other words, is there some simple way to adapt the

for line in file:

以便同時從兩個文件中提取數(shù)據(jù)?

so that it pulls data from both files simultaneously?

推薦答案

Python 2:

使用 itertools.izip 加入兩個迭代器.

Use itertools.izip to join the two iterators.

from itertools import izip
for line_from_file_1, line_from_file_2 in izip(open(file_1), open(file_2)):

如果文件長度不等,請使用 izip_longest.

If the files are of unequal length, use izip_longest.

在 Python 3 中,請改用 zipzip_longest.此外,使用 with 打開文件,這樣即使出現(xiàn)錯誤,也會自動處理關(guān)閉.

In Python 3, use zip and zip_longest instead. Also, use a with to open files, so that closing is handled automatically even in case of errors.

with open(file1name) as file1, open(file2name) as file2:
    for line1, line2 in zip(file1, file2):
        #do stuff

這篇關(guān)于如何同時遍歷兩個文件中的行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久成人人人人精品欧 | 亚洲精品视频导航 | 九九热国产精品视频 | 一级黄色毛片 | 综合伊人 | 天天av天天好逼 | 男女免费在线观看视频 | 一区二区三区在线免费观看 | 99在线精品视频 | 99爱免费 | 精品视频一区二区三区 | 成人日韩精品 | 国产精品免费在线 | 亚洲视频在线一区 | 欧美福利在线 | 国产一区2区 | 欧美日韩一区二区在线观看 | 日本午夜视频 | 成人欧美一区二区三区在线播放 | 天天曰天天干 | 拍拍无遮挡人做人爱视频免费观看 | 天天天天天操 | 国产精品亚洲成在人线 | 亚洲欧洲成人在线 | 91久久精品国产91久久性色tv | 另类 综合 日韩 欧美 亚洲 | 天天干天天操天天爽 | 久久久这里都是精品 | 日韩视频精品在线 | 中文av网站 | 9久9久| 亚洲视频免费在线看 | 看av网| 午夜在线小视频 | 天天色图 | 久久精品欧美一区二区三区不卡 | 欧美精品久久久久 | 亚洲精品影院 | 国产精品久久二区 | 欧美a级成人淫片免费看 | 911网站大全在线观看 |