I have a text file with data in it. I am trying to create a python code that will format this data in a particular way so another code I have can read it as an input. So, I am trying to remove specific lines and columns, etc. The text file contains some information at the top of the file, and then columns and columns of numerical data below it. However, I only want the numerical data. I do not want the first 34 lines of other information written in the text file.
So, my current question is: How can I remove specific lines of a text file? And how do I print this result so I can see if it worked or not?
Before you come at me: Yes, I have looked up my question on StackOverflow. I still don't get it, and I don't know what I'm doing wrong. I need your help! :)
(Image of my text file for reference)
Since each of the lines I want to remove in the text file begin with a '#', maybe it would be possible to tell the code to remove all lines that begin with "#'. Or maybe I should just specify I want the first 34 lines deleted. Either way, I'm confused on how to do this.
Separated below are all my different attempts: (Don't worry, I have imported pandas for my attempts, it's just not written here.)
with open('EBSD_data.txt','r') as f:
lines = f.readlines()
with open('EBSD_data.txt','w') as f:
for lines in lines:
if line.strip("\n").startswith('#'):
f.write(lines)
with open('EBSD_data.txt', 'r') as E:
data = E.read().splitlines(True)
with open('EBSD_data.txt', 'w') as EB:
EB.writelines(data[34:])
print(EB.read)
with open('EBSD_data.txt', 'w') as data:
for lines in data:
if not lines.startswith('#'):
data.write(lines)
with open('EBSD_data.txt', 'r') as data2:
print(data2)
x = open('EBSD_data.txt')
for line in x:
if line.startswith('#'):
del(line)
x.head()
lines = []
with open(r'EBSD_data.txt','r') as fp:
lines = fp.readlines()
with open(r'EBSD_data.txt','w') as data:
for number, line in enumerate(lines):
if number not in [34]:
fp.write(line)
With some of these attempts, it'll look like it runs fine, but then I have issues with printing my results. The typical error I am encountering after trying to print my result is:
<_io.TextIOWrapper name='EBSD_data.txt' mode='w' encoding='UTF-8'>
Any guidance you could give would be great! Thank you! :)
pandas, I can't help but suggest using theread_csvpandas function:df = pd.read_csv(skiprows=34, header=None)pandas.pydata.org/docs/reference/api/pandas.read_csv.html