python reading file returns 0x035022B0 - python

i just started using python
and i've been trying to read a file
the code doesn't return any errors but it shows this message on the
debugging screen
<_csv.reader object at 0x035022B0>
i searched but i couldn't find any answers
i even tried other sample codes to see if the problem is in my writing
but it returned the same message
*Note = the file is in the same directory of the project
this is my code
import csv
with open('nora.csv', mode='r') as CSVFile:
read = csv.reader(CSVFile, delimiter =",")
print(read)
CSVFile.close()
thank you for your help in advance

Try using pandas to read csv files - it will place the csv information in a dataframe for you.
import pandas as pd
#Place the csv data into a DataFrame
file = pd.read_csv("/path/to/file.csv")
print file

i still don't know what that message meant
but i added a for loop to read instead of just adding print
and it worked
import csv
with open('players.csv', mode='r') as CSVFile:
read = csv.reader(CSVFile, delimiter =",")
for row in read :
print(row)
CSVFile.close()
thank you guys for helping <3 <3

Related

Fix a csv with HTML content in rows with Python

I have a problem with a csv :
CSV image problem
I have HTML code in a csv but in some rows, the code broke the csv format.
I would like to know if somebody know how to solve this problem with python.
I would to fix the csv to get this format.
csv with format
I try to use with open and pandas to create a new csv
import csv
with open('csv_problem.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)

Python: Read in multiple csv files from a directory into a dictionary without using Pandas

I have a directory containing multiple csv's that I would to read into a single dictionary. The dictionary would use the original file names as keys and the contents of the csv's as values. I don't want to use pandas because I am new to Python and want to understand these tasks first before pulling out the big guns. I would like to use DictReader for the task. Here is the code I have so far below. It works fine for one file at a time. Help is greatly appreciated.
def read_lines():
data = []
with open('vari_late_low_scores.csv', newline='') as stream:
reader = csv.reader(stream, delimiter=',', skipinitialspace=True)
for row in reader:
data.append(row)
return data
Thank you!

How to append from one csv file to another?

Tried different ways. The closest way that may fit my need is the following code:
with open('list.csv', 'r') as reader, open('list-history.csv', 'a') as writer:
for row in reader:
writer.writerow(row)
I'm using 'a' and tried 'w' as well but no luck.
The result is no output at all.
Any suggestion, please? Thanks.
I think there should be a error with a stacktrace.
Here: writer.writerow(row)
Normally open() returns file object which doesn't have .writerow() method, normally, you should use .write(buffer) method.
Example
with open('list.csv', 'r') as reader, open('list-history.csv', 'a') as writer:
for row in reader:
writer.write(row)
For me it works well with test csv files. But it doesn't merge them, just appends content of one file to another one.
If both the csv columns have same name. Python's pandas module can help.
Example Code snippet.
import pandas as pd
df1 = pd.read_csv("csv1.csv")
df2 = pd.read_csv("csv2.csv")
df1.append(df2, ignore_index=True)
df1.to_csv("new.csv",index=False)

Python will not read the CSV file at all

I have tried this over and over many times and it still will not work. All that I need to do is to read a csv file, but my program will not do it. I have tried the pandas.read_csv() and it just says it does not have that attribute:
My error message
I have also tried importing csv and reading it that way but it refuses to admit that there is a file there:
My other error message
And here is my code for pandas:
pd.read_csv('data.csv')
And my code for the rest:
with open('data.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
row = next(csv_reader)
print(row)
And last but not least, here is the proof that the file exists: Proof
Do not create modules using library names. Rename pandas.py. Try again.
df= pd.read_csv('data.csv')

How write and append data in csv file and store it in list

I made csv file in my python code itself and going to append next data in ti it but the error is comming
io.UnsupportedOperation: not readable
I tried code is:
df.to_csv('timepass.csv', index=False)
with open(r'timepass.csv', 'a') as f:
writer = csv.reader(f)
your_list = list(writer)
print(your_list)
want to append next data and store in the same csv file. so that csv file having both previous and current data.
so please help me to find out..
Thanks in advance...
It is so simple just try this:
import pandas as pd
df = pd.read_excel("NSTT.xlsx","Sheet1") #reading Excel
print(df) #Printing data frame
df.to_excel("new.xlsx") #Writing Dataframe into New Excel file
Now here if you want to append data in the same file then use
df.to_excel("new.xlsx","a")
And no need to add in a list as you can directly access the data same as a list with data frame only you have to define the location .
Please check this.
You can use pandas in python to read csv and write csv:
import pandas as pd
df = pd.read_csv("csv file")
print(df)
Try:
with open(r'timepass.csv', 'r') as f:
reader = list(csv.reader(f))
print(reader)
Here you are opening your file as r, which means read-only and assigning the list contents to reader with list(csv.reader(f)). Your earlier code a opens the file for appending only where in the documentation is described as:
'a' opens the file for appending; any data written to the file is
automatically added to the end
and does not support the read().
And if you want to append data to the csv file from a different list, use the with open as a with the writer method.
with open('lake.csv','a') as f:
csv.writer(f,[1,2,3]) #dummy list [1,2,3]
Or directly from the pandas.DataFrame.to_csv method from your new dataframe, with header = False so as not to append headers:
df.to_csv('timepass.csv', index=False)
df_new.to_csv(r'timepass.csv', mode='a', header=False) #once you have updated your dataframe, you can directly append it to the same csv file
you can use pandas for appending two csv quickly
import pandas as pd
dataframe1=pd.read_csv("a.csv")
dataframe2=pd.read_csv("b.csv")
dataframe1=dataframe1.append(dataframe2)
dataframe1=dataframe1.reset_index(drop=True)
dataframe1.to_csv("a.csv")

Categories

Resources