开发者

Loops not working - Strings (Python)

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 l开发者_开发问答oops 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


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


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]


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜