I have excel sheet with Ids such as je2456,je2645,je2893,....
I would like to save it in a list in python.
But its throwing errors while importing like
'No such file or directory exists.'
Make sure that the file you are reading from is in the same folder.
import csv
def csv_reader(input_file_name):
with open(input_file_name, newline='') as csvfile:
return list(csv.reader(csvfile))
Now the call the function and save the output:
# Make sure to add the extension for the file name, whatever it may be.
my_data_list = csv_reader("Your_Input_File_Here.csv")
Now your my_data_list is a list containing all the rows from the CSV file.
Related
I'm trying to covert multiple text files into a single .csv file using Python. My current code is this:
import pandas
import glob
#Collects the files names of all .txt files in a given directory.
file_names = glob.glob("./*.txt")
#[Middle Step] Merges the text files into a single file titled 'output_file'.
with open('output_file.txt', 'w') as out_file:
for i in file_names:
with open(i) as in_file:
for j in in_file:
out_file.write(j)
#Reading the merged file and creating dataframe.
data = pandas.read_csv("output_file.txt", delimiter = '/')
#Store dataframe into csv file.
data.to_csv("convert_sample.csv", index = None)
So as you can see, I'm reading from all the files and merging them into a single .txt file. Then I convert it into a single .csv file. Is there a way to accomplish this without the middle step? Is it necessary to concatenate all my .txt files into a single .txt to convert it to .csv, or is there a way to directly convert multiple .txt files to a single .csv?
Thank you very much.
Of course it is possible. And you really don't need to involve pandas here, just use the standard library csv module. If you know the column names ahead of time, the most painless way is to use csv.DictWriter and csv.DictReader objects:
import csv
import glob
column_names = ['a','b','c'] # or whatever
with open("convert_sample.csv", 'w', newline='') as target:
writer = csv.DictWriter(target, fieldnames=column_names)
writer.writeheader() # if you want a header
for path in glob.glob("./*.txt"):
with open(path, newline='') as source:
reader = csv.DictReader(source, delimiter='/', fieldnames=column_names)
writer.writerows(reader)
Here is my first file's codes:
from csv import writer
film=[""] #The list contains informations about the film
#İnputs:
filmname=input("Film name: ")
category=input("category: ")
score=input("point: ")
score=str(score)
complement=input("complement: ")
valuablenames=input("İmportant actors: ")
#Appending Inputs
film.append(filmname)
film.append(category)
film.append(score)
film.append(complement)
film.append(valuablenames)
#The Function which appends the list into csv file:
def liste_append(filename,inf_list):
with open("FilmSet.csv", 'a+', newline='') as write_obj: # Open file in append mode
csv_writer = writer(write_obj) # Create a writer object from csv module
csv_writer.writerow(film) # Add contents of list as last row in the csv file
#Calling Function
liste_append("FilmSet.csv",film)
#Jumping to File which converts csv to xlsx
#Jumping to File which converts csv to xlsx
The part i need your help to close this file and jump to the other one (which is a converter program and named as "converter.py").So when i run this program,it will update the csv file and jump to "converter.py" for updating excel file from csv converted.( deletes the old xlsx and creates new xlsx which is converted from csv )
Best solution
My recommendation would be to turn one of the python files into a package and simply import the required functions.
So in this case put your csv conversion from conversion.py into a function like csv_to_xlsx() and then from conversion import csv_to_xlsx() and then run it at the end of your main file.
Other solutions
You could use the subprocess module with subprocess.Popen() or os.system(). These will allow you to execute python code from another file. Also the worst other solution is exec() this allows you to pass python code as a string and execute it. so you could do
exec(open('file.py', 'r').read())
I was hoping to use a CSV file with a list of file paths in one column and use Python to print the actual files.
We are using Window 7 64-bit.
I have got it to print a file directly:
import os
os.startfile(r'\\fileserver\Sales\Sell Sheet1.pdf, 'print')
The issues comes in when I bring in the CSV file. I think I'm not formatting it correctly because I keep getting:
FileNotFoundError: [WinError2] The system cannot find the file specified: "['\\\\fileserver\\Sales\\Sell Sheet1']"
This is where I keep getting hung up:
import os
import csv
with open (r'\\fileserver\Sales\TestList.csv') as csv_file:
TestList = csv.reader(csv_file, delimiter=',')
for row in TestList:
os.startfile(str(row),'print')
My sample CSV file contains:
\\fileserver\Sales\SellSheet1
\\fileserver\Sales\SellSheet2
\\fileserver\Sales\SellSheet3
Is this an achievable goal?
You shouldn't be using str() there. The CSV reader gives you a list of rows, and each row is a list of fields. Since you just want the first field, you should get that:
os.startfile(row[0], 'print')
At work, I need to carry out this process every month, which involves downloading some files from a server, copying them into a new folder, deleting the existing files, etc, which is quite mundane. I tasked myself with writing a Python script to do this.
One step of this process is opening an Excel file and saving it as a CSV file. Is there anyway of doing this Python?
EDIT:
The main difficulty I have with this is two-fold. I know how to write a CSV file using the Python's csv library, but
How do I read in Excel files into Python?
Does the result from reading in an Excel file then writing it as a CSV file coincide with opening the file in Excel, and perform save-as CSV manually?
Is there a better way of doing this then the way suggested here?
I guess I really want an answer for 1. 2 I can find out myself by using something like Winmerge...
To manipulate Excel files in Python, take a look at this question and answer, where the use of xlrd package is used. Example:
from xlrd import open_workbook
book = open_workbook('example.xlsx')
sheet = book.sheet_by_index(1)
To manipulate CSV files in Python, take a look at this question and answer and documentation, where the library csvis used. Example:
import csv
with open('example.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print ', '.join(row)
I had to do this for a project in a course I took.
You dont need to involve Excel in the process as you can simply create a csv file and then open it in any program you like.
If you know how to write to a file as txt then you can create a csv. I am not sure if this is the most efficient way but it works.
When setting up the file, instead of something like:
data = [["Name", "Age", "Gender"],["Joe",13,"M"],["Jill",14,"F"]]
filename = input("What do you want to save the file as ")
filename = filename + ".txt"
file = open(filename,"w")
for i in range(len(data)):
line = ""
for x in range (len(data[i])):
line += str(data[i][x])
line += ","
file.write(line)
file.write("\n")
file.close()
simple replace the file extension from txt to csv like this:
filename = filename + ".csv"
I'm using Python's csv module to do some reading and writing of csv files.
I've got the reading fine and appending to the csv fine, but I want to be able to overwrite a specific row in the csv.
For reference, here's my reading and then writing code to append:
#reading
b = open("bottles.csv", "rb")
bottles = csv.reader(b)
bottle_list = []
bottle_list.extend(bottles)
b.close()
#appending
b=open('bottles.csv','a')
writer = csv.writer(b)
writer.writerow([bottle,emptyButtonCount,100, img])
b.close()
And I'm using basically the same for the overwrite mode(which isn't correct, it just overwrites the whole csv file):
b=open('bottles.csv','wb')
writer = csv.writer(b)
writer.writerow([bottle,btlnum,100,img])
b.close()
In the second case, how do I tell Python I need a specific row overwritten? I've scoured Gogle and other stackoverflow posts to no avail. I assume my limited programming knowledge is to blame rather than Google.
I will add to Steven Answer :
import csv
bottle_list = []
# Read all data from the csv file.
with open('a.csv', 'rb') as b:
bottles = csv.reader(b)
bottle_list.extend(bottles)
# data to override in the format {line_num_to_override:data_to_write}.
line_to_override = {1:['e', 'c', 'd'] }
# Write data to the csv file and replace the lines in the line_to_override dict.
with open('a.csv', 'wb') as b:
writer = csv.writer(b)
for line, row in enumerate(bottle_list):
data = line_to_override.get(line, row)
writer.writerow(data)
You cannot overwrite a single row in the CSV file. You'll have to write all the rows you want to a new file and then rename it back to the original file name.
Your pattern of usage may fit a database better than a CSV file. Look into the sqlite3 module for a lightweight database.