简体   繁体   中英

Python open csv file copy to variable close file use variable?

I am trying to read a csv file, copy it, close the file and use the copy. I need to move away from the with or csv.reader block. after hours of searching i found this:

fr = open('my_symbols.csv', 'r')
csv.reader(fr)
my_symbol_list = deepcopy(fr)
fr.close()

But that does not work either. Anybody have any hints or suggestions? There must be a simple answer rigth?

Thanks in advance

Skip

#make variable outside of 'with'
filelines = []

with open('my_symbols.csv','r') as fr:
    filelines = fr.readlines()

#after with (file is now closed), use the variable.
#you may need to split up the lines further in to columns
print(filelines[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM