Loops not working - Strings (Python)

北慕城南 提交于 2020-01-06 07:28:26

问题


I need help in reading these textfiles, somehow when i do a recursive loop, the other loop always gets reset to the 1st line.

import sys
import codecs # there are actually more utf-8 char to the lines, so i needed codecs
reload(sys)
sys.setdefaultencoding('utf-8')

reader = codecs.open("txtfile1", 'r', 'utf-8')
reader2 = codecs.open("txtfile2", 'r', 'utf-8')

for row in reader:
    print row[0:11] # here the outer loops is running the cycles
    for row2 in reader2:
        print row[0:11] # here the outer loops gets resets
        if row[0:11]==row2[0:11]:
            print row[12:] + row2[12:]

The textfiles look like these:

txtfile1

95032302317 foo
95032302318 bar
95032302319 bron
95032302320 cow
95032302321 how 
95032302322 now
95032303001 lala
95032303002 lili

txtfile2

95032103318 bar (in another utf8 language)
95032103319 bron (in another utf8 language)
95032103320 cow (in another utf8 language)
95032103321 how (in another utf8 language)
95032103322 now (in another utf8 language)
95032103323 didi
95032103324 dada
95032103325 kaka

回答1:


Can't tell you why but this can be fixed by simply replacing for row in reader: with for row in reader.readlines():. If everything can't be imported at once, then you'll probably need to handle iteration manually.

EDIT

I just realized I did something slightly different in getting this to work:

outer = codecs.open(<outer loop file).readlines()
inner = codecs.open(<inner loop file).readlines()

for o in outer:
   for i in inner:
       print o



回答2:


This code snarfs the files into memory, but would probably work for you if your files are smaller than a few hundred megs.

#!/usr/bin/python2 -S
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

import sys
sys.setdefaultencoding("utf-8")
import site

import codecs

t1 = {}
t2 = {}

with codecs.open("txtfile1", 'r', 'utf-8') as reader:
    for row in reader:
        number, text = row.split(" ", 1)
        t1[number] = text

with codecs.open("txtfile2", 'r', 'utf-8') as reader:
    for row in reader:
        number, text = row.split(" ", 1)
        t2[number] = text

common = set(t1.keys()) & set(t2.keys())

while common:
    key = common.pop()
    print t1[key], t2[key]



回答3:


I'd simply do it like this:

row2 = reader2.readlines()
for row in reader.readlines():
    print row
    if row in row2:
        print 'yeah'

EDIT: new solution:

row2 = [line[:11] for line in reader2.readlines()]
for row in reader.readlines():
    print row
    if row[:11] in row2:
        print 'yeah'


来源:https://stackoverflow.com/questions/6383284/loops-not-working-strings-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!