file-io

FileNotFoundError: [Errno 2]

FileNotFoundError: [Errno 2] Question: Synopsis: How do I read a file in Python? why must it be done this way? My problem is that I get the following error: Traceback (most recent call last): File “C:UsersTerminalDesktopwkspcfiletesting.py”, line 1, in <module> testFile=open(“test.txt”) FileNotFoundError: [Errno 2] No such file or directory: ‘test.txt’ Which originates from the following …

Total answers: 1

Convert percent string to float in pandas read_csv

Convert percent string to float in pandas read_csv Question: Is there a way to convert values like ‘34%’ directly to int or float when using read_csv in pandas? I want ‘34%’ to be directly read as 0.34 Using this in read_csv did not work: read_csv(…, dtype={‘col’:np.float}) After loading the csv as ‘df’ this also did …

Total answers: 2

Python Pandas: How to read only first n rows of CSV files in?

Python Pandas: How to read only first n rows of CSV files in? Question: I have a very large data set and I can’t afford to read the entire data set in. So, I’m thinking of reading only one chunk of it to train but I have no idea how to do it. Asked By: …

Total answers: 3

Open files in 'rt' and 'wt' modes

Open files in 'rt' and 'wt' modes Question: Several times here on SO I’ve seen people using rt and wt modes for reading and writing files. For example: with open(‘input.txt’, ‘rt’) as input_file: with open(‘output.txt’, ‘wt’) as output_file: … I don’t see the modes documented, but since open() doesn’t throw an error – looks like …

Total answers: 4

How to write to a file without overwriting current contents?

How to write to a file without overwriting current contents? Question: with open(“games.txt”, “w”) as text_file: print(driver.current_url) text_file.write(driver.current_url + “n”) I’m using this code right now, but when it writes to the file it overwrites the old content. How can I simply add to it without erasing the content that’s already there. Asked By: user2540748 …

Total answers: 1

How do I read each line of a file and add if statements?

How do I read each line of a file and add if statements? Question: I’m pretty new to Python, but I’m trying to learn. One idea I had for a simple script was to have a script that reads and writes to a log file during execution. Then based on what’s in that log file, …

Total answers: 1

pandas to_csv output quoting issue

pandas to_csv output quoting issue Question: I’m having trouble getting the pandas dataframe.to_csv(…) output quoting strings right. import pandas as pd text = ‘this is “out text”‘ df = pd.DataFrame(index=[‘1’],columns=[‘1′,’2’]) df.loc[‘1′,’1’]=123 df.loc[‘1′,’2’]=text df.to_csv(‘foo.txt’,index=False,header=False) The output is: 123,”this is “”out text””” But I would like: 123,this is “out text” Does anyone know how to get this …

Total answers: 5

Python read JSON file and modify

Python read JSON file and modify Question: Hi I am trying to take the data from a json file and insert and id then perform POST REST. my file data.json has: { ‘name’:’myname’ } and I would like to add an id so that the json data looks like: { ‘id’: 134, ‘name’: ‘myname’ } …

Total answers: 6

Changing pipe separated data to a pandas Dataframe

Changing pipe separated data to a pandas Dataframe Question: I have pipe-separated values like this: https|clients4.google.com|application/octet-stream|2296| https|clients4.google.com|text/html; charset=utf-8|0| …. …. https|clients4.google.com|application/octet-stream|2291| I have to create a Pandas DataFrame out of this data, with each column given a name. Asked By: itsaruns || Source Answers: Here you go: >>> import pandas as pd >>> pd.read_csv(‘data.csv’, sep=’|’, …

Total answers: 2

writing a pytest function to check outputting to a file in python?

writing a pytest function to check outputting to a file in python? Question: I asked this question about how to write a pytest to check output in stdout and got a solution. Now I need to write a test case, to check if the contents are written to the file and that the contents are …

Total answers: 2