Read/Write 文件:按特定顺序写入行 - Python
Read/Write File : Write to line in certain order - 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_༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻
等等。
由于某种原因,当输入文件从第一行开始时,该行不会得到处理并且不会出现在结果文件中。当输入文件中的第一行留空时,输出文件如上所示。将它移到输入文件的下一行对输出文件没有影响。这是我的第一个问题。第二个是我不知道如何用数字和名称拆分输入文件的行,然后将数字移到行的前面,将名称移到输出文件中 link 的后面,
我的代码如下所示:
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 尝试失败后,我决定使用 read/write 文件方法,因为在 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
我希望它能解释我正在尝试做的事情。非常感谢所有的帮助和建议。请原谅我的任何错误。英语不是我的第一语言。
所以我注意到,如果我使用 reversed() 反转文件,它可以解决我的问题。由于某种原因 python 总是先读取 'link' 而不管 txt 文件格式。
经过一些研究后,我发现了另一种完成任务的方法,该方法使用字符串列表并且无论 txt 文件格式如何都可以工作,这意味着它适用于 link 位于包含数据的行下方或上方的实例。
这是我用来使用 reversed() 完成任务的代码:
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 的人。得到了 almost 的即时帮助和很多好的建议,所以如果你对编程还很陌生,那么这是一个检查你是否堆积如山的好地方 something.They 在 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()
我希望这会帮助其他有类似问题的人,并向他们展示解决同一问题的两种不同方法。仅供参考,有两个以上。
我的代码有几个问题。我了解其中一个,但不确定如何解决。代码用于读取文本文件。
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_༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻
等等。
由于某种原因,当输入文件从第一行开始时,该行不会得到处理并且不会出现在结果文件中。当输入文件中的第一行留空时,输出文件如上所示。将它移到输入文件的下一行对输出文件没有影响。这是我的第一个问题。第二个是我不知道如何用数字和名称拆分输入文件的行,然后将数字移到行的前面,将名称移到输出文件中 link 的后面,
我的代码如下所示:
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 尝试失败后,我决定使用 read/write 文件方法,因为在 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
我希望它能解释我正在尝试做的事情。非常感谢所有的帮助和建议。请原谅我的任何错误。英语不是我的第一语言。
所以我注意到,如果我使用 reversed() 反转文件,它可以解决我的问题。由于某种原因 python 总是先读取 'link' 而不管 txt 文件格式。 经过一些研究后,我发现了另一种完成任务的方法,该方法使用字符串列表并且无论 txt 文件格式如何都可以工作,这意味着它适用于 link 位于包含数据的行下方或上方的实例。
这是我用来使用 reversed() 完成任务的代码:
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 的人。得到了 almost 的即时帮助和很多好的建议,所以如果你对编程还很陌生,那么这是一个检查你是否堆积如山的好地方 something.They 在 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()
我希望这会帮助其他有类似问题的人,并向他们展示解决同一问题的两种不同方法。仅供参考,有两个以上。