re module doesn't save the whole text into txt file - python

Here's the code:
import re
file_name = input("Please input txt directory for your file: ")
#file_name2 = input("Please input txt directory for your file: ")
with open(file_name, 'r') as lol:
for txt_file in lol:
x = re.sub(r"[^a-zA-Z0-9]+", ' ', txt_file)
print(x)
It's going to print text without special characters. However, I need to save the printed text to a file. When I do so, it saves only the single line from txt file instead of the whole txt.
import re
file_name = input("Please input txt directory for your file: ")
#file_name2 = input("Please input txt directory for your file: ")
with open(file_name, 'r') as lol:
for txt_file in lol:
x = re.sub(r"[^a-zA-Z0-9]+", ' ', txt_file)
with open(file_name2, 'w') as p:
p.write(x)
p.close()

Your codes aren't the same; you're not writing within the loop anymore.
Did you know you can open multiple files at once?
with open(file_name, 'r') as lol, open('output.txt', 'w') as f_out:
for txt_file in lol:
x = re.sub(r"[^a-zA-Z0-9]+", ' ', txt_file)
f_out.write(x)
f_out.write('\n')

Related

How to take a input in python and change the format into txt?

This is the whole code
#input from the user
Text = input("Enter a String :) : ")
#this portion will create a file
with open(Text, 'w') as f:
file = f.write("this is a test file")
#loops through
for i in range(1,100):
#this is creating a new file
a = open(Text, "a")
b = a.write("\n this will append to the file")
a.close()
# print(i)
#this portion is reading from the file
f = open(Text, "r")
d = f.read()
print(d,end="")
f.close()
I'm trying to take the input in string, but i want it to save the file in text format
I'm a beginner, just trying things.
what i want is, that it creates a file in .txt
like,
input: helloWorld
output: helloWorld.txt
Append ".txt" to the filename the user inputs:
#input from the user
Text = input("Enter a String :) : ") + ".txt"

Go through files in given directory with python, read each file line by line and remove first and last string in the line and save updated file

So I have some .txt files inside of directory. Each .txt file contains some paths like:
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module.c'
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module2.c'
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module3.c'
I need just some small function that will go through each line of each file inside of a dir and remove there ', so only clear path is left like:
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module.c
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module2.c
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module3.c
My code at the moment is:
for filename in files:
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
if line.startswith('')and line.endswith(''):
remove('')
Please assist!
SOLUTION:
I have managed to find a solution with a bit different approach:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
Thanks!
python has a hirarchy to strings ', ", "" and so on so you can wrap a uptick into quotes for a split. Since we have the first element '' before the tick the second is your path
line.split("'")[1]
Edit: If i understood you correctly you want this
for filename in files:
paths = []
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
paths.append(line.split("'")[1])
file.close()
with open(filename, 'w') as file:
file.writelines(paths)
file.close()
Soo I just did bit different approach and managed to find a solution:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
Thanks guys anyway!

Write input file with any number of lines to tab-delimited output [duplicate]

This question already has answers here:
Why can't I call read() twice on an open file?
(7 answers)
Closed last month.
I'm trying to write a script that will take an input file with an unknown number of columns separated by commas and create a new file (name specified by user) where columns are separated by tabs.
The test input file I'm working with looks like this:
Data 1,35,42,7.34,yellow,male
Data 2,41,46,8.45,red,female
Here is the code I have so far:
# Read input file
infile = open("input_file.txt", "r")
line_count = 0
# Read as a collection, removing end line character
for line in infile:
print(line, end = "")
print("The input file contains", line_count, "lines.")
# Request user input for output file name
filename = input("Enter a name for the output file: ")
# Prompt for file name if entry is blank or only a space
while filename.isspace() or len(filename) == 0:
filename = input("Whoops, try again. Enter a name for the output file: ")
# Complete filename creation
filename = filename + ".txt"
filename = filename.strip()
# Write output as tab-delim file
for line in infile:
outfile = open(filename, "w")
outfile.write(line,"\t")
outfile.close()
print("Success, the file", filename, "has been written.")
# Close input file
infile.close()
The part that writes the output isn't working - it doesn't produce an error, but the output is blank.
You can split the lines by commas and write while adding tab(\t) chars :
with open('input_file.txt','r') as f_in, open('output_file.txt', 'w') as f_out:
for line in f_in:
s = line.strip().split(',')
for i in s:
f_out.write(i+'\t')
f_out.write('\n')
or briefly as #martineau suggested :
with open('input.txt','r') as f_in, open('output.txt', 'w') as f_out:
for line in f_in:
s = line.strip().split(',')
f_out.write('\t'.join(s) + '\n')
You may use pandas:
import pandas as pd
df = pd.read_csv("input_file.txt", sep=',',header=None)
print("The input file contains", df.shape[0], "lines.")
filename = input("Enter a name for the output file: ").strip()
# Prompt for file name if entry is blank or only a space
while filename.isspace() or len(filename) == 0:
filename = input("Whoops, try again. Enter a name for the output file: ")
#Saving to csv with | separator
df.to_csv(f'{filename}.txt', sep="\t", header=None, index=None)
print("Success, the file", filename, "has been written.")

how to apply the following function to several txt files?

Hello I have several txt files in a directory, I would like to apply the following python function to all my txt files:
file = open('folder/testing.txt', 'r',encoding='utf-8')
list_lines = []
for line in file:
list_lines.append(line.replace('-\n', ' '))
list_lines2 = []
for line in list_lines:
list_lines2.append(line.replace('-\n', ''))
list_lines3 = []
for line in list_lines2:
list_lines3.append(line.replace('\n', ''))
big_line = ''.join(list_lines3)
text_file=`open("folder/Output.txt", "w")`
print(big_line)
text_file.write(big_line)
text_file.close()
print('writing document')
In order to achieve this I tried making a function:
def change(document,encoding):
file = open(document, 'r',encoding=encoding)
list_lines = []
for line in file:
#print(line.replace('\n', ' ').replace('\r', ''))
list_lines.append(line.replace('-\n', ' '))
list_lines2 = []
for line in list_lines:
list_lines2.append(line.replace('-\n', ''))
list_lines3 = []
for line in list_lines2:
list_lines3.append(line.replace('\n', ''))
big_line = ''.join(list_lines3)
text_file = open(document+'changed', "w")
print(big_line)
text_file.write(big_line)
text_file.close()
print('writing document')
In fact my function works very well however I have a directory like this:
folder$ ls
file1.txt file2.txt file3.txt ... fileN.txt
So I would like to appreciate support to find a way to apply my funcion to all the documents in the directory all end with the txt extention thanks for the support
Applying your change function to every file ending with ".txt" in the current directory is pretty easy with glob:
import glob
for file in glob.glob("*.txt"):
change(file, "utf-8")
Although this was not the question, I cannot look at this code without suggesting this shorter version of change:
def change(document,encoding):
with open(document, 'r',encoding=encoding) as file:
list_lines = [line.replace('-\n', ' ').replace('\n', '') for line in file]
big_line = ''.join(list_lines)
print(big_line)
with open(document+'changed', "w") as text_file:
text_file.write(big_line)
print('writing document')

Python: Issue when trying to read and write multiple files

This script reads and writes all the individual html files in a directory. The script reiterates, highlight and write the output.The issue is, after highlighting the last instance of the search item, the script removes all the remaining contents after the last search instance in the output of each file. Any help here is appreciated.
import os
import sys
import re
source = raw_input("Enter the source files path:")
listfiles = os.listdir(source)
for f in listfiles:
filepath = os.path.join(source+'\\'+f)
infile = open(filepath, 'r+')
source_content = infile.read()
color = ('red')
regex = re.compile(r"(\b in \b)|(\b be \b)|(\b by \b)|(\b user \b)|(\bmay\b)|(\bmight\b)|(\bwill\b)|(\b's\b)|(\bdon't\b)|(\bdoesn't\b)|(\bwon't\b)|(\bsupport\b)|(\bcan't\b)|(\bkill\b)|(\betc\b)|(\b NA \b)|(\bfollow\b)|(\bhang\b)|(\bbelow\b)", re.I)
i = 0; output = ""
for m in regex.finditer(source_content):
output += "".join([source_content[i:m.start()],
"<strong><span style='color:%s'>" % color[0:],
source_content[m.start():m.end()],
"</span></strong>"])
i = m.end()
outfile = open(filepath, 'w')
outfile.seek(0, 2)
outfile.write(output)
print "\nProcess Completed!\n"
infile.close()
outfile.close()
raw_input()
After your for loop is over, you need to include whatever is left after the last match:
...
i = m.end()
output += source_content[i:]) # Here's the end of your file
outfile = open(filepath, 'w')
...

Categories