Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
88 views
Welcome To Ask or Share your Answers For Others

1 Answer

With "data.dat" as your file:

with open("data.dat", "r") as msg:
    data = msg.readlines()

output = 0

for line in data:
    with open(str(output)+"_data.dat", "w") as msg:
        for i, char in enumerate(line.strip().split()):
            msg.write("x%s %s %s*2
" % (str(i+1),char,char))
    output += 1

You read your file in a list, you read the lines one by one, you write a new file each line. You split your line and build your string to write in the file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...