Home > other >  Python OS system how to read TXT data
Python OS system how to read TXT data

Time:09-30

As title, I ask you thank

CodePudding user response:

Method are many, the simplest is the open directly, readlines
 

File_data=https://bbs.csdn.net/topics/open (' dd2. TXT ', 'r', encoding='utf8')
List2=file_data. Readlines ()
Data_list2=[n.s trip (' \ n \ r '). The upper () for n list2 in [1]]
File_data. Close ()
Print (len (data_list2))

CodePudding user response:

reference 1/f, old coconut response:
method are many, the simplest is the open directly, readlines
 

File_data=https://bbs.csdn.net/topics/open (' dd2. TXT ', 'r', encoding='utf8')
List2=file_data. Readlines ()
Data_list2=[n.s trip (' \ n \ r '). The upper () for n list2 in [1]]
File_data. Close ()
Print (len (data_list2))

thank you, I need to store TXT file in where? Or to put it to save as utf8? How to identify TXT

CodePudding user response:

Like where goes, not in the program path, specified path, such as

File_data=https://bbs.csdn.net/topics/open (' d:/data/dd2. TXT ', 'r', encoding='utf8')


='utf8 encoding' parameter utf8 encoding of the file is read, may also be GBK.


Can not add, with the system default encoding,

File_data=https://bbs.csdn.net/topics/open (' dd2. TXT ', 'r')

The so-called TXT, come in, you read, see is clear, and notepad to open the same effect,

  • Related