String Python:文本文件如何替换多行中的不同字符串?

String Python:文本文件如何替换多行中的不同字符串?,string,python-3.x,replace,String,Python 3.x,Replace,基本任务:将URL请求转换为文本,并将其转储到文本文件(几乎是可用的CSV) 目标:一个干净的CSV。在多行中,我尝试替换多个(不同)字符: 括号, tildes(~), 在每行末尾加上逗号 我找不到任何相对简单的例子来实现这一点。寻找可以逐行循环并替换的东西 请注意:我希望这个文件会随着时间的推移而变大,所以内存不友好 以下是创建该文件的代码: import urllib.request with urllib.request.urlopen(URL1) as response: da

基本任务:将URL请求转换为文本,并将其转储到文本文件(几乎是可用的CSV)

目标:一个干净的CSV。在多行中,我尝试替换多个(不同)字符:

括号, tildes(~), 在每行末尾加上逗号

我找不到任何相对简单的例子来实现这一点。寻找可以逐行循环并替换的东西

请注意:我希望这个文件会随着时间的推移而变大,所以内存不友好

以下是创建该文件的代码:

import urllib.request
with urllib.request.urlopen(URL1) as response:
    data = response.read()
decoded_data = data.decode(encoding='UTF-8')

str_data = str(decoded_data)
saveFile = open("test.txt",'w')
saveFile.write(str_data)
saveFile.close()
这是文件中的一个简化示例,第一行有字段名,第二行和第三行表示记录

[[“F1”、“F2”、“F3”、“F4”、“F5”、“F6”]

[“string11”、“string12”、“string13”、“s~ring14”、“string15”、“string16”]


[“string21”、“string22”、“s~ring23”、“string24”、“string25”、“string26”]

您可以使用一个简单的for循环来迭代文件。然后可以替换每行中的字符

file = open("text.txt", "r")
clean_txt = ""
for line in file:
    line = line.replace("~", "").replace("[","").replace("]","")
    line[len(line)-1] = "" #Replace the last character of the line.
file.close
w = open("text.txt", "w")
w.write(clean_txt)
w.close

您可以使用一个简单的for循环来迭代文件。然后可以替换每行中的字符

file = open("text.txt", "r")
clean_txt = ""
for line in file:
    line = line.replace("~", "").replace("[","").replace("]","")
    line[len(line)-1] = "" #Replace the last character of the line.
file.close
w = open("text.txt", "w")
w.write(clean_txt)
w.close

如果要替换字符串开头或结尾的字符,请使用。如果要删除的字符具有任意位置,请改用:
line.replace(“~”,”)
。请注意,与剥离不同,不能在一个调用中指定多个字符,但可以将它们链接起来,如下所示:
line.replace(“~”,”).replace(“,”).replace(“,”).replace(“[”,”)

只是一个可能适合您的快速模型:

with open("text.txt", 'r') as f:
    with open("result.txt", 'w') as new_f:
        for line in f:
            new_line = line.strip(" [],\n\t\r").replace("~","")
            print(new_line)
            new_f.write(new_line+"\n")

因为我看到波浪号可以在任何地方,括号和逗号通常出现在末尾。我还在
条带
中添加了“\n”、“\t”、“\r”和空格,因为这些字符可能(至少,“\n”肯定会)出现在每行末尾。

如果要替换字符串开头或结尾的字符,请使用。如果要删除的字符具有任意位置,请改为使用,如下所示:
line.replace(“~”,”)
。请注意,与
strip
不同,您不能在一个
replace
调用中指定多个字符,但可以将它们链接起来,如下所示:
line.replace(“~”,”).replace(“,”).replace(“,”).replace(“[”,”)

#!/usr/bin/env python3

# Note, I used the print function as a way to visually confirm the code worked.
# the URL_call will yield a byte that has serialized data for a basic table (columns and rows, where first row are column names -- just like Excel or SQL)

URL_call = ("http://www.zzz.com/blabla.html")

# URLIB module & function: the request has to be first decoded from UTF-8
import urllib.request
with urllib.request.urlopen(URL_call) as response:
    URL_data = response.read()

URL_data_decoded = URL_data.decode(encoding='UTF-8')

# use json to convert decoded response into a python structure (from a JSON structure)
import json
URL_data_JSON = json.loads(URL_data_decoded)

# pandas will transition the python data structure from a "list-like" array to a table.
import pandas as pd
URL_data_panda = pd.DataFrame(URL_data_JSON)

# this will create the text (in this case a CSV) file
URL_data_panda.to_csv("test.csv")

# The file will need the first row removed (columns are indexed coming out of the panda)

#determine line count
num_lines = sum(1 for line in open("test.csv"))

print(num_lines)

# the zero position is assigned to the first row of text. Writing from the second row (indexed as 1) get the removal done.
lines = open("test.csv").readlines()
open("test2.csv","w").writelines(lines[1:(num_lines)])


# Changes the name of the first column from zero to a normalized name.

import fileinput

# Note, below you could setup a back-up file, in the file input, by adding an extra argument in the parens ("test2.csv", inplace=True, backup='.bak')
with fileinput.FileInput("test2.csv", inplace=True) as file:
    for line in file:
        print(line.replace("0,", "REC_NUM,"), end='')
只是一个可能适合您的快速模型:

with open("text.txt", 'r') as f:
    with open("result.txt", 'w') as new_f:
        for line in f:
            new_line = line.strip(" [],\n\t\r").replace("~","")
            print(new_line)
            new_f.write(new_line+"\n")

因为我看到波浪号可以在任何地方,括号和逗号通常出现在末尾。我还在
条带
中添加了“\n”、“\t”、“\r”和空格,因为这些字符可能(至少,“\n”肯定会)出现在每行末尾。

感谢您的输入。它实际上删除了文件的所有内容。我在发布之前尝试了这种方法。当我让它工作时,它只会在第一行“执行手术”。“正在查找将通过该文件的内容。感谢您的输入。它实际上删除了该文件的所有内容。我在发布之前尝试了这种方法。当我让它工作时,它只会在第一行“执行手术”。“正在查找将通过该文件的内容。是的,这样做了。太好了!谢谢!!:-)哇。处理波浪号和括号。找到括号进入文本文件的原始原因。URL指向一个JSON,用于传递数据表(因此列和行)。问题是我找不到一个可靠的例子来展示它。下面我用更正重新发布了我的代码。请注意,上面的“洗涤器”不在我更正的代码中。是的,这样做了。太好了!谢谢!!:-)哇。处理两个波浪线和方括号。找到了方括号进入文本文件的原始原因。URL指向一个JSON,该JSON旨在传递一个数据表(因此列和行)。问题是我找不到一个可靠的示例来显示它。下面我重新发布了我的代码,并进行了更正。请注意,上面的“洗涤器”不在我更正的代码中。
#!/usr/bin/env python3

# Note, I used the print function as a way to visually confirm the code worked.
# the URL_call will yield a byte that has serialized data for a basic table (columns and rows, where first row are column names -- just like Excel or SQL)

URL_call = ("http://www.zzz.com/blabla.html")

# URLIB module & function: the request has to be first decoded from UTF-8
import urllib.request
with urllib.request.urlopen(URL_call) as response:
    URL_data = response.read()

URL_data_decoded = URL_data.decode(encoding='UTF-8')

# use json to convert decoded response into a python structure (from a JSON structure)
import json
URL_data_JSON = json.loads(URL_data_decoded)

# pandas will transition the python data structure from a "list-like" array to a table.
import pandas as pd
URL_data_panda = pd.DataFrame(URL_data_JSON)

# this will create the text (in this case a CSV) file
URL_data_panda.to_csv("test.csv")

# The file will need the first row removed (columns are indexed coming out of the panda)

#determine line count
num_lines = sum(1 for line in open("test.csv"))

print(num_lines)

# the zero position is assigned to the first row of text. Writing from the second row (indexed as 1) get the removal done.
lines = open("test.csv").readlines()
open("test2.csv","w").writelines(lines[1:(num_lines)])


# Changes the name of the first column from zero to a normalized name.

import fileinput

# Note, below you could setup a back-up file, in the file input, by adding an extra argument in the parens ("test2.csv", inplace=True, backup='.bak')
with fileinput.FileInput("test2.csv", inplace=True) as file:
    for line in file:
        print(line.replace("0,", "REC_NUM,"), end='')