سؤال

In my code I have python opening a text document and printing a HTML document in to it. by doing so it gives me a long sentence with ";" in it

Example of text in the text document

Total:0.01358324;BTC:0.00000000;LTC:0.00000001;BC:0.00000000;NMC:0.00000000;DOGE:63.71131428;NAUT:0.00017886;DRK:0.00000000;VTC:0.00013866;FTC:0.00000000;CURE:0.00025881;PPC:0.00000000;CHARITY:0.00000000;GHS:0.00003646;GHSOCT:0.00000000;GHSAUG:0.00000000;SCRYPT:200.09600305

How would I write or edit the file so every ";" forces a new line. I currently use this line to write a file called file.

file.write(response.read().decode('utf-8'))
هل كانت مفيدة؟

المحلول

replace with a newline char.

 file.write(response.read().decode('utf-8').replace(";","\n"))

In [72]: line = "Total:0.01358324;BTC:0.00000000;LTC:0.00000001;BC:0.00000000;NMC:0.00000000;DOGE:63.71131428;NAUT:0.00017886;DRK:0.00000000;VTC:0.00013866;FTC:0.00000000;CURE:0.00025881;PPC:0.00000000;CHARITY:0.00000000;GHS:0.00003646;GHSOCT:0.00000000;GHSAUG:0.00000000;SCRYPT:200.09600305"

In [73]: print (line.replace(";","\n"))
Total:0.01358324
BTC:0.00000000
LTC:0.00000001
BC:0.00000000
NMC:0.00000000
DOGE:63.71131428
NAUT:0.00017886
DRK:0.00000000
VTC:0.00013866
FTC:0.00000000
CURE:0.00025881
PPC:0.00000000
CHARITY:0.00000000
GHS:0.00003646
GHSOCT:0.00000000
GHSAUG:0.00000000
SCRYPT:200.09600305

نصائح أخرى

You can split on ; and use the following:

with open("input.txt", "r") as inp, open("output.txt", "w") as out:
    data = inp.read().decode('utf-8').split(";")
    for line in data:
        out.write("%s\n" % line)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top