I have a text file consisting of multiline (hundreds of lines actually) strings. Each of the strings starts with '&' sign. I want to change my text file in a way that only the first 300 characters of each string remain in the new file. How I can do this by using python?
Asked
Active
Viewed 111 times
3 Answers
0
You can read a file and loop over the lines to do what you want. Strings are easily slicable in python to get the first 300 to write to another file.
file = open(path,"r")
lines = file.readlines()
newFile = open(newPath,"w")
for index, line in enumerate(lines):
newLine = line[0:301]
newFile.writelines([newLine])
Hope this is what you meant

Andries Nooteboom
- 36
- 5
0
You could do something like this:
# Open output file in append mode
with open('output.txt', 'a') as out_file:
# Open input file in read mode
with open("input.txt", "r") as in_file:
for line in in_file:
# Take first 300 characters from line
# I believe this works even when line is < 300 characters
new_line = line[0:300]
# Write new line to output
# (You might need to add '\n' for new lines)
out_file.write(new_line)
print(new_line)

madmax
- 61
- 5
0
You can use the string method split
to split your lines, then you can use slices to keep only the 300 first characters of each split.
with open("oldFile.txt", "rt") as old_file, open("newFile.txt", "wt") as new_file:
for line in old_file.read().split("&"):
new_file.write("&{}\n".format(line[:300]))
This version preserves ends of line \n
within your strings.
If you want to remove ends of line in each individual string, you can use replace
:
with open("oldFile.txt", "rt") as old_file, open("newFile.txt", "wt") as new_file:
for line in old_file.read().split("&"):
new_file.write("&{}\n".format(line.replace("\n", "")[:300]))
Note that your new file will end with an empty line.
Another note is, depending on the size of your file, you may rather use a generator function version, instead of split
which results in the whole file content being loaded in memory as a list of strings.

Whole Brain
- 2,097
- 2
- 8
- 18
-
Thankyou very mych brain for your answer. It worked for mey EXCEPT that I also want to retain the character "&" in each 300 characters. It should be Included for each entry and not Excluded. – ammad aslam Jun 16 '22 at 08:32
-
Thankyou very mych brain for your answer. It worked for me EXCEPT that I also want to retain the character "&" in each 300 characters. It should be Included for each entry and not Excluded. – ammad aslam Jun 16 '22 at 08:34
-
Do you mean that these `&` are not line separators or that you want to include them back in the beginning of your lines ? – Whole Brain Jun 16 '22 at 08:38
-
I want to include them back in the begining of my lines so that each line consists of 300 characters, including '&' . – ammad aslam Jun 16 '22 at 08:48
-
The most straightforward way to do it is to include them back when formatting (and taking one less character to keep a length of 300). I updated my answer. – Whole Brain Jun 16 '22 at 08:54
-
You are incredible Brain. It really worked for me., Ohhhhh you programmers. You guys are so incredible. Thank you very much again. – ammad aslam Jun 16 '22 at 09:10