Issue
import pandas
import sys
import os
import csv
import string
f=open(myfile,"r")
lines=f.readlines()
x=[]
for i in lines:
x.append(i.split()[3]);
f.close()
if not os.path.exists(path):
os.chdir(subfolder)
method 1 : this one works, but prints the numbers with separated digits in columns as you see in the image here : .output
with open('x.txt', 'w') as f:
writer = csv.writer(f, delimiter='\n')
writer.writerows(x)
method 2 : this one gives the error : 'list' object has no attribute 'write'
#for item in x:
# x.write("%s\n" % item)
method 3 : this one works, but writes the list with all commas in one row, while I need a column of numbers
#file = open("x.txt","w")
#
#file.write(str(x))
#
#
#file.close()
method 4 : Error : 'str' object has no attribute 'write'
for i in x:
i.write(i, '\n')
mthod 5 : Error : name 'number' is not defined
with open('x.txt', 'w') as f:
f.write('%d' % number)
I am frustrated with these errors, I wanted to use python to save my time, I wasted all that time in debugging and now I am almost at the brink of finishing it. I will appreciate your attention.
Hi All
I have a text file that contains 7 columns and I need to write each column of numbers to a separate text file. numbers with many decimal digits. So I am trying to write to a text file from a list in python, as you see, the name of each corresponding list is the same as the text file it should be written to. but I am not succeeding despite having tried several existing examples from the internet, and I don't know the reason of the errors. Why not once for all clarify it?
Solution
Since you are importing pandas, why don't you use it, too.
data = pd.read_table(myfile, header=None, sep='\s+')
data.columns = 't','x','y','z','Rx','Ry','Rz'
for column in data:
with open(column + ".txt", "w") as ofile:
ofile.write(' '.join(str(i) for i in data[column]))
Answered By - DYZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.