Getting Attrbute error _exit_ when use "with" keyword - python

I have passed .csv file to post request,
input_file = data.get('file', None)
with input_file as datasheet:
header = datasheet.readline()
Always I am getting error on second line. Also my file type is Unicode thats why it again giving error on third line for readline()

>>> with "test1.html" as fp:
... header = fp.readline()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>>
How to Read file with with stament:
code:
>>> with open("test1.html") as fp:
... header = fp.readline()
...
Check file is exits or not before doing any process.
Use os module
Demo:
>>> os.path.isfile("test1.html")
True
>>> os.path.isfile("nofile.html")
False
>>>
File Upload to server via post request in API testing using tastypie
fp = open("C:\sample_datasheet.csv", 'rb')
content = fp.read()
fp.close()
fd ={'file': "C:\sample_datasheet.csv", "content": content}
self.assertHttpOK(self.api_client.post('api of upload', format='json',\
org_id=2, content_type="multipart/form-data",\
data=fd))
and Save content from the data to server location in the view.

Considering that {u'file': u'C:\\sample_datasheet.csv'} is returned by the data.get() function, you have to obtain the file name and open it:
data = data.get('file', None)
fname = data["file"]
with open(fname, "r") as datasheet:
header = datasheet.readline()

Related

serialize a text file into a protobuf message

I have a serialized protobuf message that I can simply read and save in plain text in python with something like this:
import MyMessage
import sys
FilePath = sys.argv[1]
T = MyMessage.MyType()
f = open(FilePath, 'rb')
T.ParseFromString(f.read())
f.close()
print(T)
I can save this to a plain txt file and do what I want to do.
Now I need to do the inverse operation, i.e. reading the simple plain text file, already formatted in the right way, and save it as a protobuf message
import MyMessage
import sys
FilePath = sys.argv[1]
input = open("./input.txt", 'r')
T = MyMessage.MyType()
T.ParseFrom(inputText.readlines())
output.write(T.SerializeToString())
input.close()
output.close()
This fails with
Traceback (most recent call last):
File "MyFile.py", line 13, in <module>
T.ParseFromString(input.readlines())
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\message.py", line 199, in ParseFromString
return self.MergeFromString(serialized)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1142, in MergeFromString
serialized = memoryview(serialized)
TypeError: memoryview: a bytes-like object is required, not 'list'
I am not a python nor a protobuf expert, so I guess I am missing something trivial...
Any help?
Thanks :)
print(x) calls str(x), which for protobufs uses the human-readable "text format" representation.
To read back from that format, you can use the google.protobuf.text_format module:
from google.protobuf import text_format
def parse_my_type(file_path):
with open(file_path, 'r') as f:
return text_format.Parse(f.read(), MyMessage.MyType())

argument 1 must have a "write" method - creating csv file from json

I am making a call to the AWS API using boto3 and Python and I am writing the JSON response to a JSONfile. I am then trying to convert the JSON file to a CSV file. When I attempt to do this with the csv writer() method, I get the above error and I am not sure why.
Code:
def ResponseConvert():
dynamo = boto3.client('dynamodb')
response = dynamo.scan(
TableName='XXXX'
)
with open('vuln_data.json', 'w') as outfile:
json.dump(response, outfile, indent=4)
f = open('vuln_data.json')
data = json.load(f)
f.close()
f = csv.writer(open('vuln_data.csv', 'wb+'))
f.writerow(data.keys())
for row in data:
f.writerow(row.values())
ResponseConvert()
Traceback:
Traceback (most recent call last):
File "response_convert.py", line 21, in <module>
ResponseConvert()
File "response_convert.py", line 19, in ResponseConvert
f.writerow(row.values())
AttributeError: 'unicode' object has no attribute 'values'
CSV writers expect a file handle, not a filename.
with open('filename.csv', 'w') as f:
writer = csv.writer(f)
...
You probably want a DictWriter instead, by the way. Don't rely on the order of keys and values matching up.

Weird error opening a file in python

I am trying to read a file path using config parser and later read from that file
>>> cfg_file = './crawler.config'
>>> config = SafeConfigParser()
>>> config.read(cfg_file)
['./crawler.config']
>>> f = config.get('default', 'sites_file')
>>> with open(f) as fp:
... print fp.read()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: u'"/home/avi/src/typo.csv"'
>>>
I think there is a problem with the Unicode. I can't figure out a solution. If I pass the filename directly as a string it works fine. Any help in resolving this would be appreciated.
Try changing the value of 'sites_file' in the config to /home/avi/src/typo.csv from "/home/avi/src/typo.csv"
or:
Replace the quotes before opening the file.
Ex:
with open(f.replace('"', '')) as fp:
print fp.read()
f = config.get('default', 'sites_file')
may be f's value will be "/home/avi/src/typo.csv"
which does not exist
with open(f) as fp:
print fp.read()
You are trying to read a non existing file

How to read/print the ( _io.TextIOWrapper) data?

With the following code I want to > open a file > read the contents and strip the non-required lines > then write the data to the file and also read the file for downstream analyses.
with open("chr2_head25.gtf", 'r') as f,\
open('test_output.txt', 'w+') as f2:
for lines in f:
if not lines.startswith('#'):
f2.write(lines)
f2.close()
Now, I want to read the f2 data and do further processing in pandas or other modules but I am running into a problem while reading the data(f2).
data = f2 # doesn't work
print(data) #gives
<_io.TextIOWrapper name='test_output.txt' mode='w+' encoding='UTF-8'>
data = io.StringIO(f2) # doesn't work
# Error message
Traceback (most recent call last):
File "/home/everestial007/PycharmProjects/stitcher/pHASE-Stitcher-Markov/markov_final_test/phase_to_vcf.py", line 64, in <module>
data = io.StringIO(f2)
TypeError: initial_value must be str or None, not _io.TextIOWrapper
The file is already closed (when the previous with block finishes), so you cannot do anything more to the file. To reopen the file, create another with statement and use the read attribute to read the file.
with open('test_output.txt', 'r') as f2:
data = f2.read()
print(data)

Type Error opening python file for reading

I'm running into an issue trying to open a text file for reading in Python 3. The code is as follows:
def main():
the_file = input('What is the name of the file?')
open_file = open(the_file,"r","utf8")
open_file.read()
and then I'm calling the function.
and the error I get is:
Traceback (most recent call last):
File "/Users/Matthew/Desktop/CaesarCipher.py", line 9, in <module>
main()
File "/Users/Matthew/Desktop/CaesarCipher.py", line 7, in main
open_file = open(encrypted_file,"r","utf8")
TypeError: an integer is required
It's unclear to me where I am using an incorrect type... can I get some insight as to why this isn't working?
Thank you in advance.
The 3rd argument to open() is buffering:
open(file, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True, opener=None) -> file object
Pass the character encoding as a keyword parameter instead:
with open(the_file, encoding="utf-8") as file:
text = file.read()
This solved the issue:
open_file = open(the_file,"r")
The third parameter is a buffer parameter, not a encoding?
So what you could do is:
open_file = open(the_file,"r", 1, 'utf-8') # 1 == line Buffered reading
Also..
You should do this instead:
with open(the_file, 'rb') as fh:
data = fh.read().decode('utf-8')
or
with open(the_file, 'r', -1, 'utf-8') as fh:
data = fh.read()
Cleaner, you get "control" over the decoding and won't end up with open filehandles or wrongful encodings.

Categories

Resources