Python Forum
Help me modify this .txt into .csv with Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me modify this .txt into .csv with Python
#2
lines = texte.split('&')

is wrong, your splitter is &&. & is on begin and end only.

in_text = ""
out_text = ""

# open file, read and split
with open("data_groupe_11-12_B.txt", "r") as f:
    in_text = f.read()[1:-1].split("&&")

# for replacements
repl = ["title", "year", "duration", "score", "nbvotes", "revenue", "revenue", "restriction", "desc"]

# set headers
out_text = ";".join(repl).title()
out_text += "\n"

for line in in_text:
    line = line.replace(",", ";")
    
    for ch in repl:
        if ch in line:
            # replacing, (for example title= year= ...)
            line=line.replace(f"{ch}=", "").replace("  ", "").strip()
    
    out_text += f"{line}\n"
        
    
print(out_text)

# save as semicolon seperated text file
with open("result.csv", "w") as f:
    f.write(out_text)
Reply


Messages In This Thread
RE: Help me modify this .txt into .csv with Python - by Axel_Erfurt - Dec-14-2022, 01:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,251 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  I am writing a car rental program on python. In this function I am trying to modify aboo 2 2,805 Aug-05-2021, 06:00 PM
Last Post: deanhystad
  modify the color a string of characters in python 3.6 atlass218 10 5,704 Feb-28-2019, 03:20 PM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020