How to replace the last line ina text file? - python

I have a program that runs twice a day however I am rewriting it as I'm making a new update for my program. I'm running into a problem. The program writes about 2500 different text files each time it runs.
Each file is a csv and has five columns
date, data, data, data, data
Im wanting to delete the last row of the data to write the new information only if it has already ran that day.
with open('file.csv', 'r+') as file:
info = [line.split(',') for line in file]
for row in info:
if str(today) in row:
#need help here
this is the old one that I'm re-doing. I need to rework it as it will no longer work with the new program.
with open('file.csv', 'a+') as file:
file.write(str(today) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' + '\n')

Maybe this will help you
with open('file.csv', 'r+') as file:
info = [line.split(',') for line in file]
for idx, row in enumerate(info):
if str(today) in row:
# replace with new today data
info[idx] = [str(today), str(data), str(data), str(data), str(data)]
with open('file.csv', 'w+') as file:
for row in info:
file.write(str(today) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' + '\n')

Related

Create two different xml files from single csv file using python

I am pretty new to python and I have one csv file and based on one condition I need to create two different xml files.
The condition based on which I need to create two xml is as follows:
If Primary spindle == 1 then data related to it will go to xml1 else xml2.
This is how my xml looks like as shown in image:
The code I am writing is as follows:
import csv
file = open(r'C:\Users\hu170f\MultiSpindleArchitechture\New folder\MAAP-S12_LH_UP_PASS-LN1-V1.csv')
csvreader = csv.reader(file)
xmlFile = r'C:\Users\hu170f\Documents\TEST\myData4.xml'
xmlFile1 = r'C:\Users\hu170f\Documents\TEST\myData5.xml'
#header = next(csvreader)
xmlData = open(xmlFile, 'w')
xmlData.write('<?xml version="1.0" encoding = "UTF-8"?>' + "\n")
# there must be only one top-level tag
xmlData.write('<MCD>' + "\n")
xmlData1 = open(xmlFile1, 'w')
xmlData1.write('<?xml version="1.0" encoding = "UTF-8"?>' + "\n")
# there must be only one top-level tag
xmlData1.write('<MCD>' + "\n")
#print(header)
rows = []
for row in csvreader:
rows.append(row)
#print(len(rows))
#print(len(row))
Field = " "
Value = " "
counter = 0
for i in rows:
tag = i
#print(tag)
#print(len(rows))
for j in range(len(tag)):
tag[j] = tag[j].replace(' ', '_')
if j == 0:
#print("Field = ", tag[j])
Field = tag[j]
counter = counter +1
else:
#print("Value = ", tag[j])
Value = tag[j]
if(counter%2 == 0):
xmlData.write(' ' + '<' + Field + '>' \
+ Value + '</' + Field + '>' + "\n")
else :
xmlData1.write(' ' + '<' + Field + '>' \
+ Value + '</' + Field + '>' + "\n")
xmlData.write('</MCD>' + "\n")
xmlData.close()
xmlData1.write('</MCD>' + "\n")
xmlData1.close()
#print(rows[6])
file.close()
I want both xml to contain common data from Header to LN and then data from WorkStep UUID till secondary Spindle based on the condition in each xml file.

Python 3: Attempting to iterate thru csv file and write contents to text file

I am trying to read each line of the csv file below.
csv file
I would like to extract the data from each row and append to a new text file.
Here is the code i use to read the csv file:
#Count number of lines in the csv file
cnd = datetime.now().strftime("%m%d%y")
df = pd.read_csv(cnd+'sr.csv')
numline = len(df)
ma = df['Make'][:numline]
mo = df['Model'][:numline]
yr = df['Year'][:numline]
cl = df['Color'][:numline]
pr = df['Price'][:numline]
#For loop to break the csv file into 10 lines at a time
for x in range(0,numline,10):
if x < 10:
mk = ma[:10]
mod = mo[:10]
ye = yr[:10]
clr = cl[:10]
prc = pr[:10]
I'm using the snippet of code below to create the new text file, extract the data from the csv file and appended the data to the new text file. The problem lies in the 3rd block of code.
#Create text File
facts = datetime.now().strftime("%m-%d-%y-%H%M%S.txt")
f = open(facts, 'w+')
#Write generic Title
f.write('Car Facts' + '\r')
f.close()
#Open the text file. Iterate thru each line of the csv file and append to the text file
of = open(facts, "a")
for k in mk:
for m in mod:
for y in ye:
for c in clr:
for p in prc:
of.write('\r' + 'Make: ' + str(k) + '\r' + 'Model: ' + str(m) + '\r' + 'Year: ' + str(y) + '\r' + 'Color: ' + str(c) + '\r' + 'Price: ' + str(p) + '\r')
of.close()
I end up with a text file that has about 30 blocks of random combinations.
The very first and last block written to the text file are correct.
Everything else in between is just random combinations that i don't need.
How can I fix the code to get the desired end result below?
Thank you
Car Facts
Make: Ford
Model: Bronco
Year: 1978
Color: Blue
Price: $24,000.00
Make: Land Rover
Model: Defender
Year: 1985
Color: Black
Price: $21,000.00
This opens the existing CSV file and loops through it while writing the line to the new text file. You might have to manually delete the first entry since it consists of the titles
File1 = open("Existing CSV File.csv", "r")
File2 = open("New text file.txt", "w")
for X in File1:
Line = X.split(",")
File2.write("Make: " + Line[0] + "\n")
File2.write("Model: " + Line[1] + "\n")
File2.write("Year: " + Line[2] + "\n")
File2.write("Colour: " + Line[3] + "\n")
File2.write("Price: " + Line[4] + "\n")
File2.write("")
File1.close()
File2.close()

change and miss columns from text file in python

I have a text file with below data and trying to change columns and write into another file.
Here the input is
a|b|c|d|e|f
d|f|g|h|y|s
Like that i have 1000 rows
Now i want to swap the columns and also don't want all the columns and then write into another file.
The output i need is - I don't need 2 columns from the input
c|d|a|b
g|h|d|f
I tried like below and it's not working
with open("old.txt","r") as file,open('new.txt', 'w') as outfile:
freader = csv.reader(file, delimiter = '|')
for line in file:
line[3], line[4],line[1],line[2]
outfile.write(line)
Any help is more appreciated.
import csv
with open('old.csv', 'r') as f_in, open('new.csv', 'w') as f_out:
freader = csv.reader(f_in, delimiter = '|')
fwriter = csv.writer(f_out, delimiter = '|')
for line in freader:
fwriter.writerow([line[2], line[3], line[0], line[1]])
File old.csv:
a|b|c|d|e|f
d|f|g|h|y|s
File new.csv:
c|d|a|b
g|h|d|f
for line in file: while this is valid syntax, you should use the cvsreader that you created. Replace this with for line in freader:
line[3], line[4],line[1],line[2] This line does nothing, you should store it in a variable. Replace this with newline = line[2] + "|" + line[3] + "|" + line[0] + "|" + line[1]. This builds a string with | in between each character.
outfile.write(line) You are writing the same line that you read. Replace this with outfile.write(newline + "\n"). This will write the string we created to the file. Adding \n will add a newline at the end of each line
with open("old.txt","r") as file,open('new.txt', 'w') as outfile:
freader = csv.reader(file, delimiter = '|')
for line in freader:
newline = line[2] + "|" + line[3] + "|" + line[0] + "|" + line[1]
outfile.write(newline + "\n")

not writing all lines of output in a new file in python

I am creating a file and I want to write all lines of write_line to my output.
With this could I have a new file but only with the last line of write_log not all the lines. I think I should have a for before written log and tell to write all, but i am so new with python and need help.
I am getting name / familtname / id by SOAP response. I want to print responses which are in lines, now i just see the last line not all the lines.
timestamp = str(datetime.datetime.now())[:19]
file = open(CreateFile, 'w')
write_line = str(name).strip() + ';' + familyname.strip() + ';' + str(id).strip() + ';' + timestamp
file.writelines(write_line + '\n')
def CreateFile():#******************creating output log file*****
today = str(datetime.date.today()).split('-')
NowTime = str(datetime.datetime.now())[11:19:]
Nowtime_split = NowTime.split(':')
timestamp=Nowtime_split[0]+Nowtime_split[1]+Nowtime_split[2]
daystamp=today[0]+today[1]+today[2]
filename = 'log' + '_' + daystamp + '_' + timestamp + '.csv'
destfile = r'C:\Desktop' + str(filename)
file = open(destfile, 'w')
file.close()
return(destfile)
CreateFile=CreateFile()
this is a small case:
import datetime
timestamp = str(datetime.datetime.now())[:19]
file = open('1.txt', 'w')
for i in range(10):
write_line ='try'+str(i)
file.writelines(write_line + '\n')
file.close()
`
I'm not really sure what you want but I think the problem is because you're using write parameter to open the file and it's always replacing the previous text, so what you can do is replacing write with append(a):
timestamp = str(datetime.datetime.now())[:19]
with open(CreateFile, 'a') as file:
write_line = str(name).strip() + ';' + familyname.strip() + ';' +str(id).strip() + ';' + timestamp
file.write(write_line + '\n')
I suggest you to use with open... in order to avoid closing the file opened and other futures errors
lines = ['line1', 'line2', ...] # set of lines (list) you want to add in the current timestamp file
with open('current_timestampfile.txt', 'w') as f:
f.writelines("%s\n" % l for l in lines)

Python: How can I create a data file from a converted csv file?

I am trying to convert a csv file into another file (file type doesn't matter as the program using the converted data just opens it like a text file).
So far I have managed to convert and print the original csv data into the the data structure I want but I now need to save that as another file.
import csv
file = open('newData', 'w')
with open('initialData.csv', 'rb') as f:
reader = csv.reader(f, delimiter=',', quotechar='|')
for row in reader:
print row[13] + ' 1:' + row[0] + ' 2:' + row[1]
file.write(f)
file.close()
Whenever I run this I get the error:
TypeError: expected a character buffer object
I know there is nothing wrong with converting the csv file as that prints fine when I comment out the file.write(f).
Many thanks in advance!
Why are you trying to write the original file (the f object) to the new file? Don't you want to write the re-formatted data?
import csv
with open('initialData.csv', 'rb') as f_in, open('newData', 'w') as f_out:
reader = csv.reader(f_in, delimiter=',', quotechar='|')
for row in reader:
print row[13] + ' 1:' + row[0] + ' 2:' + row[1]
f_out.write(row[13] + ' 1:' + row[0] + ' 2:' + row[1])
Edit: as suggested by Jon Clements, use context manager for output as well + indentation fix.
You're trying to print out the file handle for the whole csv file. I'm guessing you want the text you're printing to be written out into a file, in that case just do:
with open('initialData.csv', 'rb') as infile, open('newData.txt') as outfile:
reader = csv.reader(infile, ...)
for row in reader:
outfile.write(row[13] + ' 1:' + row[0] + ' 2:' + row[1])

Categories

Resources