这是一个蟒蛇初学者。
我想出了一个小任务,所以我可以继续学习和改进我的python。我有一个示例file1:
Name: Group: UID:
user1 group1 12
user2 group2 23
user3 group3 34
user9 group9 99
我正在尝试在file2列中实现结果:
User_Name: User_ID:
user1 12
user9 99
我已经走到了这一步,接近结尾是我被困的地方:
#!/usr/bin/python3.3
with open("file1") as rfile:
with open("file2", "w") as wfile:
for line in rfile:
l=line.strip()
if l:
x=l.split()
#From here, I am not sure what to do next.
if "user1" in x:
do something...
if "user9" in x:
do something..
感谢您的帮助。
答案 0 :(得分:0)
你可以做到
wfile.write( x[0] + "\t" + x[2] + "\n")