<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### **Python練手小片段** **1.** 實際需求:兩個文件(csv或txt),每行最多15個項目,‘;’號為分隔符。 為方便導入pandas處理,進行以下處理(選擇進行一個個多個操作): * 移除文本文件的首行 * 合并兩個文本文件 * ‘;’號替換為‘,’號 * 格式化為每行15個項目,不足15個項目的行,添加‘000’以達到15個項目的目的。 **1.1** 移除文本文件的首行 ~~~ def remove_1st_line(infile): """Remove the 1st line of a txt file.""" file_str = '' with open(infile, 'r') as f: next(f) # Skip header line for line in f: file_str += line with open(infile, 'w') as f: f.write(file_str) ~~~ 使用with open方式打開文件,然后使用next()跳過首行。把文件剩余內容寫入一個大字符串。再寫入文件。 針對不大的文本文件比較有效。 --- **1.2** 合并兩個文本文件 ~~~ def merge(folder, output_f_name): """Merge 2 (or more) txt/csv file to 1.""" # Obtain file name list merge_file_dir = os.getcwd() + f'\\{folder}\\' filenames = os.listdir(merge_file_dir) print(f'---合并以下文件: {filenames}') name, ext = os.path.splitext(filenames[0]) # CSV file is gb2312 generate by scale, TXT is utf-8, for our file if ext == '.csv': code = 'gb2312' else: code = 'utf-8' # Open a new file, put content of filenames list into it with open(output_f_name, 'w', encoding=code) as f: # Get the filepath for open it, for filename in filenames: filepath = merge_file_dir + filename # For each file, write it line by line to output file with open(filepath, 'r', encoding=code) as f1: temp = f1.readlines() f.writelines(temp) return output_f_name ~~~ 需要合并的文件放在當前工作目錄下的文件夾內。返回文件名以供其他函數調用。此程序是根據實際文件編寫,code是判斷本人使用的文件。csv與txt分別是不同機器生成的文件。編碼不同。 ___ **1.2** 替換 ~~~ def alter(file_name, old_str, new_str): """Replace a str with another within a txt file""" with open(file_name, 'r', encoding='utf-8') as f: file_date = f.readlines() for i, value in enumerate(file_date): file_date[i] = file_date[i].replace(old_str, new_str) with open(file_name, 'w', encoding='utf-8') as f: f.writelines(file_date) if __name__ == '__main__': alter('kkk.txt', ';', ',') ~~~ 替換文本文件內指定字符串為新字符串。 for循環內使用enumerate的原因:如果直接使用列表值迭代,在for循環內對列表值的更新并不能直接生效,達到更改列表的目的。需要引入一個中間值列表。不值得。 --- **1.3** 生成15個項目的行(意思就是14個逗號) ~~~ def format15(file_name): """Format each line of a text file to 15 items for importing into pandas""" with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() for i, value in enumerate(lines): lines[i] = lines[i].split(',') lines[i][-1] = lines[i][-1].strip() for j in range(15 - len(lines[i])): lines[i].append('000') lines[i] = ','.join(lines[i]) + '\n' with open(file_name, 'w', encoding='utf-8') as f: f.writelines(lines) ~~~ 基于同樣的原因使用enumerate對列表值進行原地修改。生成的文件導入padas試試。 pandas讀取一行一行的文本時,如果一行項目是15,另一行項目小于15,就報錯。這是寫個小片段的原因。 --- 2. 待 3. 定
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看