How to Convert tdms file to text or csv in python - python-3.x

Here i tried nptdms module but giving error
tdms_file = TdmsFile.as_dataframe('file.tdms',time_index=False, absolute_time=False, scaled_data=True)
AttributeError: 'str' object has no attribute 'groups'
please anyone have idea to convert tdms file csv or text file ,
here i don't know what is inside the file

You need to use the filepath and read in the actual file. Right now you are trying to read in the string 'file.tdms' which is merely 9 letters -- so you get the 'str' object error. Find the actual filepath for the file.tdms and you should be fine. It could look something like this:
filepath = '/home/x/y/file.tdms'
tdms_file = TdmsFile.as_dataframe(filepath,time_index=False, absolute_time=False, scaled_data=True)

Related

how to get filename given FileData object in Python?

I have a FileData object file, where when I print(file) I will get:
<FileData ID_0a0eebac2.dcm (ID_0a0eebac2.dcm)>
I am hoping to extract the name of the file, "ID_0a0eebac2.dcm", from this object. How can I do this?
This way you can do it
In python the name is an attribute of IOWrapper : 'https://docs.python.org/3/library/io.html'
file = open('ID_0a0eebac2.dcm')
#to get the name of the file you can do this
file_name = file.name # the name of the file is saved as an attribute of the object
If you want to make a function
def get_file_name(file):
return file.name
I hope you got your answer

PyPDF2 - Byte Data vs Binary Data - TypeError

I am trying to get print one page of a PDF to a new PDF document. I am using the following code:
from PyPDF2 import PdfFileReader, PdfFileWriter
file_path = "/file_path/.pdf"
input_pdf = PdfFileReader(file_path)
output_file = PdfFileWriter()
cover_page = input_pdf.getPage(0)
output_file.addPage(cover_page)
with open("portion.pdf", "wb") as output_file:
output_file.write(output_file)
When I run this code I get the following error:
Traceback (most recent call last):
File ".../Extract a portion of PDF.py", line 18, in <module>
output_file.write(output_file)
TypeError: a bytes-like object is required, not '_io.BufferedWriter'
I have specified that the output needs to write binary, so why is it saying that I must use byte-like objects?
Cheers,
In the with statement, you named the opened file output_file. This essentially reassigned output_file from a PdfFileWriter() to the file stream you just opened. When you tried to do output_file.write(output_file), that's basically trying to write the file stream object itself into the file stream, which makes no sense and causes the TypeError.
To fix this, simply rename the variable you used in the with statement:
with open("portion.pdf", "wb") as output_file_stream:
output_file.write(output_file_stream)
Alternatively, you can also rename the PdfFileWriter() to output_pdf instead of output_file and change the with statement to something like:
with open("portion.pdf", "wb") as output_file:
output_pdf.write(output_file)
which might make more sense.

How to convert Navigable String to File Object

I am trying to get some data from a website (using the modules named requests & BeautifulSoup) and print it in a text file but every time I try to do so, it says the following:
TypeError: descriptor 'write' requires a 'file' object but received a 'NavigableString'
I have tried using the csv library to import the data but since I couldn't add the line by line data to the csv, I decided to add all the output to a text file and then take out the data I require.
file_object = open("name-list.txt", "w") #Opening the file
name = soup.find(class_='table-responsive') #Extracting the data
name_list = name.find_all('td') #Refining the data
for final in name_list:
all = final.contents[0] #Final result
file.write(all) #This is where the Error Comes
file.close()
When I use print(all) in the for loop, I get the output that I need which consists of multi-line text including the names, age, gender, etc. of the people from the table on the website but when I try to print that output into the text file, the error pops up.

How to write tagged string into text file in python?

I am using POS tagging, parse chunking and deep parsing in one step using TextBlob. I want to write the output in a txt file. The error I get is
"TypeError: a bytes-like object is required, not 'TaggedString'"
Following is the code I am using. The out variable contains information like this.
Technique:Plain/NNP/B-NP/O and/CC/I-NP/O enhanced-MPR/JJ/I-NP/O CT/NN/I-NP/O chest/NN/I-NP/O is/VBZ/B-VP/O
from textblob import TextBlob
with open('report1to8_1.txt', 'r') as myfile:
report=myfile.read().replace('\n', '')
out = TextBlob(report).parse()
tagS = 'taggedop.txt'
f = open('taggedop.txt', 'wb')
f.write(out)
I also want to split these tags into dataframe in this way.

Python3 open text file

I am trying to open a simple text file using
dir_path = os.path.dirname(os.path.realpath(__file__))
F = open(dir_path+"\\sankey2.txt","r")
The path to the file is :C:\Users\David\Google Drive\Jobs\1.Efe\Transporte\0.MODEL
Inside the text file is the following text :
sankey2.txt
$SETGLOBAL REFERENCIA
Now if I run python it gives me the error :
TypeError: decoding to str: need a bytes-like object, _io.TextIOWrapper found
So I tried the following 2 solutions :
Trying this
F = bytes(open(dir_path+"\\sankey2.txt","r"))
I get
TypeError: 'str' object cannot be interpreted as an integer
Trying this
F = open(dir_path+"\\sankey2.txt","rb")
I get
TypeError: decoding to str: need a bytes-like object, _io.BufferedReader found
It is not entirely clear to me how to solve this problem. Encoding to UTF-8 doesn't help either.
Thanks for your help.

Resources