简体   繁体   English

Python 打开 csv 文件复制到变量关闭文件使用变量?

[英]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.我正在尝试读取 csv 文件,复制它,关闭文件并使用副本。 I need to move away from the with or csv.reader block.我需要远离 with 或 csv.reader 块。 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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