2015-02-11 58 views
-2

我的代码有几个问题。我了解其中一个,但不知道如何解决它。代码是为了读取文本文件。读/写文件:按特定顺序写入行 - Python

格式的TXT文件:

204 jack sparrow 

http://testlink.com/test123 

123 Doughboy® 

http://testlink.com/test346 

348 ༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

http://testlink.com/testr55 

等..

接下来应该写另一个文件与输出如下:

输出文件格式:

204 http://testlink.com/test123&u_link=jack_sparrow 

123 http://testlink.com/test346&u_link=Doughboy® 

348 http://testlink.com/testr55&u_link=༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

等等......

我的输出如下:

204 jack_sparow 

http://testlink.com/test123&u_link=123_Doughboy® 

http://testlink.com/test346&u_link=348_༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

等等。

由于某些原因,当输入文件从第一行开始时,该行不会得到处理,并且不会出现在结果文件中。当第一行在输入文件中留空时,输出文件如上所示。将它移动到输入文件中的下一行,在输出文件中没有区别。这是我的第一个问题。第二个是,我不知道如何分割输入文件的行号和名称,然后将数字移到行的前面,并命名到输出文件中的链接后面,

我的代码如下所示:

for line in open('test2.txt'): #reading file 

rec = line.strip() 

rec = rec.replace(" ", "_") #Need whitespaces and brackets removed from link so i replaced them with low line 
rec = rec.replace("(", "_") 
rec = rec.replace(")", "_") 

level = ('1', '2', '3', '4', '5', '6', '7', '8', '9') #line with number and name always starts with number 

link = ('h')   #line with link always starts with letter h as in http:// 

name = (rec[3:])  

if rec.startswith(link): 

    f = open("test5.txt","a") 

f.write(rec + "&u_link=")  #writes link and append $u_link= to the end of the line and this is the place where i want to append the name 

if rec.startswith(level) : 

    f = open("test5.txt","a") 

    f.write(rec + "\n\n")  # this is where i write name and number 

我知道代码远非完美,但我刚开始我的编程冒险,这是我第二次尝试完成相同的任务。在我的raw_input尝试失败之后,我决定使用读/写文件方法,原因是名称中包含的符号和花哨字体无法在Windows命令行中处理,但在Linux控制台上运行良好(Windows中的cmd使用的编码方式与utf不同-8)。

这是我第一次尝试代码工作得很好,但中继手工输入,而不是文件:

print "level?",  
level = raw_input()  # file should be sorted by this variable 
print "link?", 
link = raw_input()  
print "name?",   # Problem with fonts and symbols 
name = raw_input() 
name = name.replace(" ", "") #This removes spaces from the name as URL   cant have spaces 
ul = "&u_link="  #This have to be appended to the link followed by the name 
el = "\n"    #Empty line to separate links in test.txt file 
f = open("test.txt","a") 
f.write(el+level+" -- "+link+ul+name+el) #file writing 
print level+" -- "+link+ul+name   #printing in the console just to see if works 

我希望这解释了什么是我想要做的事。所有的帮助和建议非常感谢。请原谅我的任何错误。英语不是我的第一语言。

回答

0

所以我注意到,如果我使用reverse()反转文件,它可以修复我的问题。由于某些原因,无论txt文件格式如何,python都会首先阅读“链接”。 经过一小段研究,我发现完成任务的另一种方式是使用字符串列表并工作,而不考虑txt文件格式,这意味着它适用于链接位于包含数据或其上的行的实例。

这里是我用来完成任务使用逆转()的代码:

import os 
import glob 

for line in reversed(open("test2.txt").readlines()): 
    rec = line.strip() 
    rec = rec.replace("<", "_") 
    rec = rec.replace(">", "_") 
    rec = rec.replace("&", "n") 
    rec = rec.replace(" ", "_") 
    rec = rec.replace("(", "_") 
    rec = rec.replace(")", "_") 
    rec = rec.replace('"', "_") 
    rec = rec.replace("'", "_") 
    level = ('1', '2', '3', '4', '5', '6', '7', '8', '9') 
    link = ('h') 
    if rec.startswith(link): 
    f = open("temp.txt","a") 
    f.write(rec + "&u_link=") 
    elif rec.startswith(level) : 
    f = open("temp.txt","a") 
    f.write(rec + "\n\n") 
    f.close() 
for line in reversed(open("temp.txt").readlines()): 
    lines = line.strip()  
    f = open("hitlistlinks.txt","a") 
    f.write(lines + "\n")  

files = glob.glob('temp.txt') 
for f in files: 
    os.remove(f) 

请注意,我在过程中,我和删除创建的临时文件:在结束

files = glob.glob('temp.txt') 
for f in files: 
    os.remove(f) 

我码。为了这种方式工作,我不得不导入os和glob方法。

现在我对解决方案并不完全满意,所以我做了更多的研究。 最后,我写了另一个代码,从http://www.reddit.com/r/learnprogramming/ 一些帮助强烈推荐从Learnprogrammin @reddit的家伙。得到几乎即时的帮助和很多好的建议,所以如果你是一个相当新的编程,这是一个很好的地方检查,如果你堆叠东西。他们也有freenode #Learnprogramming非常活跃的IRC频道。

这是最后的代码,更清洁,做这项工作:

# Open the file 
with open("test3.txt", "r") as f: 

# Here we're going to clean up the input file to wipe out 
# any whitespace at the beginning or end of each line 
    cleaned_lines = [] 
    for line in f: 
     cleaned_lines.append(line.strip()) 

# Now we'll recombine it back into a single string of text 
# with the lines separated by the \n character 
    all_text = "\n".join(cleaned_lines) 

# Split the text on blank lines. Groups should now be a list 
# of strings, where each group contains two adjacent lines 
# that contain a link and a strip of data 
    groups = all_text.split("\n\n") 

# Now we'll go through each group and break it apart into the 
# two separate lines. One of them will start with an "http" 
# and that one will be our link. 

    for group in groups: 

     line1, line2 = [x for x in group.split("\n") if x] 
     if line1.startswith("http"): 
      link = line1 
      rec = line2 
     elif line2.startswith("http"): 
      link = line2 
      rec = line1 
     else: 
     # If one of the two lines doesn't start with "http" we 
     # have a group that doesn't have a link. 
     # I'll just throw 
     # an error and bring the program to a halt. 
      raise Exception("This group is missing a link! format(group)) 

     # At this point the link variable contains the link, and 
     # the data variable contains the other line. Now we can process the input file as intended 
     # and it will work on either file. 
     rec = rec.replace("<", "_") 
     rec = rec.replace(">", "_") 
     rec = rec.replace("&", "n") 
     rec = rec.replace(" ", "_") 
     rec = rec.replace("(", "_") 
     rec = rec.replace(")", "_") 
     rec = rec.replace('"', "_") 
     rec = rec.replace("'", "_") 
     f = open("hitlist.txt","a") 
     f.write(link + "&u_link=" + rec + "\n\n") 
     f.close() 

我希望这将帮助其他有类似的问题,并告诉他们两种不同的方法同样的问题。仅供参考有两个以上。

相关问题